Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 110: Lec 4: Functions and Recursion

Similar presentations


Presentation on theme: "EECS 110: Lec 4: Functions and Recursion"— Presentation transcript:

1 EECS 110: Lec 4: Functions and Recursion
Aleksandar Kuzmanovic Northwestern University

2 Functioning in Python # my own function! def dbl( x ):
""" returns double its input, x """ return 2*x

3 Functioning in Python Some of Python's baggage… # my own function!
def dbl( x ): """ returns double its input, x """ return 2*x keywords Some of Python's baggage… def starts the function return stops it immediately and sends back the return value Docstrings They become part of python's built-in help system! With each function be sure to include one that Comments describes overall what the function does, and explains what the inputs mean/are They begin with #

4 >>> undo('caf') >>> undo(undo('caf'))
Functioning in Python def undo(s): """ this "undoes" its string input, s """ return 'de' + s >>> undo('caf') >>> undo(undo('caf'))

5 Conditional Statements
ax**2 + bx + c = 0 If b**2 – 4*a*c is less than 0, then the equation has no solution If b**2 – 4*a*c is equal to 0, then the equation has 1 solution If b**2 – 4*a*c is greater than 0, then the equation has 2 solutions

6 Striving for simplicity…
def qroots(a,b,c): """ qroots(a,b,c) returns a list of the real-number solutions to the quadratic equation ax**2 + bx + c = 0 input a: a number (int or float) input b: a number (int or float) input c: a number (int or float) """ if b**2 - 4*a*c < 0: return [ ] if b**2 - 4*a*c == 0: return [-b/(2*a)] else: return [(-b-(b**2 - 4*a*c)**0.5)/(2*a), (-b+(b**2 - 4*a*c)**0.5)/(2*a)] # If b**2 – 4*a*c is less than 0 # then the equation has no solution # If b**2 – 4*a*c is equal to 0 # then the equation has one solution # Otherwise, the equation has 2 solutions

7 Simpler to fix, if needed!
Naming data == Saving time def qroots(a,b,c): """ qroots(a,b,c) returns a lost of the real-number solutions to the quadratic equation ax**2 + bx + c = 0 input a: a number (int or float) input b: a number (int or float) input c: a number (int or float) """ d = b**2 - 4*a*c if d < 0: return [ ] if d == 0: return [ -b/(2*a) ] r1 = (-b + d**0.5)/(2*a) r2 = (-b - d**0.5)/(2*a) if r1 > r2: return [ r2, r1 ] else: return [ r1, r2 ] Simpler to fix, if needed! Faster to run, as well…

8 Simpler to fix, if needed!
Naming data == Saving time def qroots(a,b,c): """ qroots(a,b,c) returns a lost of the real-number solutions to the quadratic equation ax**2 + bx + c = 0 input a: a number (int or float) input b: a number (int or float) input c: a number (int or float) """ d = b**2 - 4*a*c if d < 0: return [ ] if d == 0: return [ -b/(2*a) ] r1 = (-b + d**0.5)/(2*a) r2 = (-b - d**0.5)/(2*a) if r1 > r2: return [ r2, r1 ] else: return [ r1, r2 ] Name once - use often! Simpler to fix, if needed! Faster to run, as well…

9 Functioning Python… Docstrings become part of python's help system
# data gets named on the way IN to a function def qroots(a,b,c): """ qroots(a,b,c) returns a lost of roots to the quadratic equation ax**2 + bx + c = 0 input a: a number (int or float) input b: a number (int or float) input c: a number (int or float) """ d = b**2 - 4*a*c if d < 0: return [ ] if d == 0: return [ -b/(2*a) ] r1 = (-b + d**0.5)/(2*a) r2 = (-b - d**0.5)/(2*a) if r1 > r2: return [ r2, r1 ] else: return [ r1, r2 ] # notice that we don't get to name data on the way out… function defs must be at the very left Docstrings become part of python's help system Indenting indicates code "blocks" qroots "block" is 11 lines

10 How functions work… What is demo(-4) ? def demo(x): return x + f(x)
def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x vault 713? What is demo(-4) ?

11 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x vault 713? What is demo(-4) ?

12 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x/2) vault 713? What is demo(-4) ?

13 How functions work… These are different x's ! What is demo(-4) ?
def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x/2) These are different x's ! vault 713? What is demo(-4) ?

14 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(-4) + g(-4/2) g vault 713? x = -4 What is demo(-4) ? return -1.0 * x

15 How functions work… What is demo(-4) ? def demo(x): return x + f(x)
return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* g(-4/2) g vault 713? x = -4 What is demo(-4) ? return -1 * -4 4

16 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* 4 + g(-4/2) vault 713? What is demo(-4) ?

17 How functions work… What is demo(-4) ? def demo(x): return x + f(x)
return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* g(-4/2) g vault 713? x = -2 What is demo(-4) ? return -1 * -2 2

18 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* vault 713? What is demo(-4) ?

19 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* 46 vault 713? What is demo(-4) ?

20 42 How functions work… What is demo(-4) ? def demo(x): demo
return x + f(x) demo x = -4 return def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x vault 713? What is demo(-4) ? 42

21 Thinking sequentially
factorial 5! = 120 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1

22 Recursion == self-reference!
Thinking recursively Recursion == self-reference! factorial 5! = 120 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1 N! = N * (N-1)!

23 Warning This is legal code! def fac(N): return N * fac(N-1)

24 Warning def fac(N): return N * fac(N-1)
No base case -- the calls to factorial will never stop! Make sure you have a base case, then worry about the recursive step...

25 Thinking recursively ! def fac(N): if N <= 1: return 1 Base Case

26 Thinking recursively ! def fac(N): if N <= 1: return 1 else:
return N * fac(N-1) Base Case Recursive Step Human: Base case and 1 step Computer: Everything else

27 The base case is No Problem!
def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(1) Result: 1 The base case is No Problem!

28 Behind the curtain… fac(5) def fac(N): if N <= 1: return 1 else:
return N * fac(N-1) Behind the curtain… fac(5)

29 Behind the curtain… fac(5) 5 * fac(4) def fac(N): if N <= 1:
return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4)

30 Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) def fac(N):
if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3)

31 Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2)
def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2)

32 Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1)
def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1)

33 Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1)
def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(5) "The Stack" 5 * fac(4) 4 * fac(3) 3 * fac(2) Remembers all of the individual calls to fac 2 * fac(1) 1

34 Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * 1
def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * 1

35 Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * 2 def fac(N):
if N <= 1: return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4) 4 * fac(3) 3 * 2

36 Behind the curtain… fac(5) 5 * fac(4) 4 * 6 def fac(N): if N <= 1:
return 1 else: return N * fac(N-1) Behind the curtain… fac(5) 5 * fac(4) 4 * 6

37 Behind the curtain… fac(5) 5 * 24 def fac(N): if N <= 1: return 1
else: return N * fac(N-1) Behind the curtain… fac(5) 5 * 24

38 Let recursion do the work for you.
Recursion Mantra Let recursion do the work for you.

39 Let recursion do the work for you.
Recursion Mantra Let recursion do the work for you. Exploit self-similarity Less work ! Produce short, elegant code

40 Let recursion do the work for you.
Recursion Mantra Let recursion do the work for you. Exploit self-similarity Less work ! Produce short, elegant code def fac(N): if N <= 1: return 1 else: return N * fac(N-1) You handle the base case – the easiest possible case to think of! Recursion does almost all of the rest of the problem!

41 You handle the base case – the easiest possible case to think of!
One Step But you do need to do one small step… def fac(N): if N <= 1: return 1 else: return fac(N) You handle the base case – the easiest possible case to think of! This will not work

42 Breaking Up… s = "this has 2 t's" L = [ 21, 5, 16, 7 ]
is easy to do with Python. s = "this has 2 t's" How do we get at the initial character of s? How do we get at ALL THE REST of s? L = [ 21, 5, 16, 7 ] How do we get at the initial element of L? How do we get at ALL the REST of L?

43 Breaking Up… s = "this has 2 t's" s[0] L = [ 21, 5, 16, 7 ]
is easy to do with Python. s = "this has 2 t's" s[0] How do we get at the initial character of s? How do we get at ALL THE REST of s? L = [ 21, 5, 16, 7 ] How do we get at the initial element of L? How do we get at ALL the REST of L?

44 Breaking Up… s = "this has 2 t's" s[0] s[1:] L = [ 21, 5, 16, 7 ]
is easy to do with Python. s = "this has 2 t's" s[0] How do we get at the initial character of s? How do we get at ALL THE REST of s? s[1:] L = [ 21, 5, 16, 7 ] How do we get at the initial element of L? How do we get at ALL the REST of L?

45 Breaking Up… s = "this has 2 t's" s[0] s[1:] L = [ 21, 5, 16, 7 ] L[0]
is easy to do with Python. s = "this has 2 t's" s[0] How do we get at the initial character of s? How do we get at ALL THE REST of s? s[1:] L = [ 21, 5, 16, 7 ] L[0] How do we get at the initial element of L? How do we get at ALL the REST of L?

46 Breaking Up… s = "this has 2 t's" s[0] s[1:] L = [ 21, 5, 16, 7 ] L[0]
is easy to do with Python. s = "this has 2 t's" s[0] How do we get at the initial character of s? How do we get at ALL THE REST of s? s[1:] L = [ 21, 5, 16, 7 ] L[0] How do we get at the initial element of L? How do we get at ALL the REST of L? L[1:]

47 Recursion Examples def mylen(s): """ input: any string, s
output: the number of characters in s """

48 Recursion Examples def mylen(s): """ input: any string, s
output: the number of characters in s """ if s == '': return else:

49 Recursion Examples def mylen(s): """ input: any string, s
output: the number of characters in s """ if s == '': return 0 else: return 1 + mylen( s[1:] ) Will this work for lists?

50 Behind the curtain… mylen(‘eecs')

51 Behind the curtain… mylen(‘eecs') 1 + mylen('ecs')

52 Behind the curtain… mylen(‘eecs') 1 + mylen('ecs') 1 + mylen('cs')

53 Behind the curtain… mylen(‘eecs') 1 + mylen('ecs') 1 + mylen('cs') 1 + mylen('s')

54 Behind the curtain… mylen(‘eecs') 1 + mylen('ecs') 1 + mylen('cs') 1 + mylen('s') 1 + mylen('')

55 Behind the curtain… mylen(‘eecs') 1 + mylen('ecs') 1 + mylen('cs') 1 + mylen('s') 1 + mylen('') = 0

56 Recursion Examples def mymax(L): """ input: a NONEMPTY list, L
output: L's maximum element """

57 Recursion Examples def mymax(L): """ input: a NONEMPTY list, L
output: L's maximum element """ if len(L) == 1: return else:

58 Recursion Examples def mymax(L): """ input: a NONEMPTY list, L
output: L's maximum element """ if len(L) == 1: return L[0] else: if L[0] < L[1]: return mymax( L[1:] ) return mymax( L[0:1] + L[2:] )

59 Behind the curtain… mymax( [1,7,3,42,5] )

60 sajak('wheel of fortune') == 6
"Quiz" on recursion Names: def power(b,p): def sajak(s): """ returns b to the p power using recursion, not ** inputs: ints b and p output: a float """ """ returns the number of vowels in the input string, s """ UOIAUAI power(5,2) == 25.0 sajak('wheel of fortune') == 6 Want more power? Handle negative values of p, as well. For example, power(5,-1) == (or so)

61 def power(b,p): if p == 0: return if p > 0: else: # p < 0
""" inputs: base b and power p (an int) implements: b**p = b*b**(p-1) """ if p == 0: return if p > 0: else: # p < 0

62 def power(b,p): if p == 0: return 1 if p > 0: return else:
""" inputs: base b and power p (an int) implements: b**p = b*b**(p-1) """ if p == 0: return 1 if p > 0: return else: # p < 0

63 def power(b,p): if p == 0: return 1 if p > 0: return b*power(b,p-1)
""" inputs: base b and power p (an int) implements: b**p = b*b**(p-1) """ if p == 0: return 1 if p > 0: return b*power(b,p-1) else: # p < 0 return

64 def power(b,p): if p == 0: return 1 if p > 0: return b*power(b,p-1)
""" inputs: base b and power p (an int) implements: b**p = b*b**(p-1) """ if p == 0: return 1 if p > 0: return b*power(b,p-1) else: # p < 0 return 1/power(b,-1*p)

65 behind the curtain power(2,3)

66 def sajak(s): Base case? Rec. step?
when there are no letters, there are ZERO vowels Base case? Look at the initial character. if it is NOT a vowel, the answer is Rec. step? if it IS a vowel, the answer is

67 def sajak(s): Base case? Rec. step?
when there are no letters, there are ZERO vowels Base case? Look at the initial character. if it is NOT a vowel, the answer is just the number of vowels in the rest of s Rec. step? if it IS a vowel, the answer is 1 + the number of vowels in the rest of s

68 Checking for a vowel: Try #1
def sajak(s): if len(s) == 0: return 0 else: Base Case Checking for a vowel: Try #1

69 Checking for a vowel: Try #1
def sajak(s): if len(s) == 0: return 0 else: Base Case Checking for a vowel: Try #1 and or same as in English! but each side has to be a complete boolean value! not

70 Checking for a vowel: Try #1
def sajak(s): if len(s) == 0: return 0 else: Base Case if s[0] == 'a' or s[0] == 'e' or… Checking for a vowel: Try #1 and or same as in English! but each side has to be a complete boolean value! not

71 Checking for a vowel: Try #2
def sajak(s): if len(s) == 0: return 0 else: Base Case in Checking for a vowel: Try #2

72 def sajak(s): if len(s) == 0: return 0 else: if s[0] not in 'aeiou':
return sajak(s[1:]) return 1+sajak(s[1:]) Base Case Rec. Step if it is NOT a vowel, the answer is just the number of vowels in the rest of s if it IS a vowel, the answer is 1 + the number of vowels in the rest of s

73 behind the curtain sajak('eerier')

74 Good luck with Homework #1
The key to understanding recursion is to first understand recursion… - advice from a student


Download ppt "EECS 110: Lec 4: Functions and Recursion"

Similar presentations


Ads by Google