EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
-
Upload
josephine-harrell -
Category
Documents
-
view
220 -
download
0
Transcript of EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
![Page 1: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/1.jpg)
EECS 110: Lec 4: Functions and Recursion
Aleksandar Kuzmanovic
Northwestern University
http://networks.cs.northwestern.edu/EECS110-s15/
![Page 2: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/2.jpg)
Raising and razing lists
What are
"Quiz"pi = [3,1,4,1,5,9] Q = [ 'pi', "isn't", [4,2] ]
What slice of pi is [3,4,5]
What is pi[pi[2]]?
message = 'You need parentheses for chemistry !'
What is message[::5]
What are
pi[0] * (pi[1] + pi[2])
and
pi[0] * (pi[1:2] + pi[2:3])
What is message[9:15]
What are
What slice of pi is [3,1,4]
How many nested pi's before pi[…pi[0]…] produces an error?
Name(s):
Extra! Mind Muddlers
Part 2Part 1Q[0]
Q[0:1]
Q[0][1]
Q[1][0]
len(pi)
len(Q)
len(Q[1])
![Page 3: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/3.jpg)
Raising and razing lists
What are
"Quiz"pi = [3,1,4,1,5,9] Q = [ 'pi', "isn't", [4,2] ]
What slice of pi is [3,4,5]
What is pi[pi[2]]?
message = 'You need parentheses for chemistry !'
What is message[::5]
What are
pi[0] * (pi[1] + pi[2])
and
pi[0] * (pi[1:2] + pi[2:3])
What is message[9:15]
What are
What slice of pi is [3,1,4]
How many nested pi's before pi[…pi[0]…] produces an error?
Name(s):
Extra! Mind Muddlers
Part 2Part 1Q[0]
Q[0:1]
Q[0][1]
Q[1][0]
len(pi)
len(Q)
len(Q[1])
![Page 4: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/4.jpg)
>>> 3*'i' in 'alien'
False
The in thing
>>> 'i' in 'team'
False
>>> 'cs' in 'physics'
True
>>> ‘sleep' not in ‘EECS 110'
True
>>> 42 in [41,42,43]
True
>>> 42 in [ [42], '42' ]
False
a little bit different for lists…
![Page 5: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/5.jpg)
Functioning in Python
Some basic, built-in functions:
abs
max
min
sum
range
round
bool
float
int
long
list
str
these change data from one type to another
absolute value
of lists
creates lists
only as accurately as it can!
helpThe most important: dir
![Page 6: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/6.jpg)
Far more are available in separate files, or modules:
import math
math.sqrt( 1764 )
dir(math)
from math import *
pi
sin( pi/2 )
accesses math.py's functions
lists all of math.py's functions
same, but without typing math. all of the time…
Functioning in Python
![Page 7: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/7.jpg)
# my own function!
def dbl( x ):
""" returns double its input, x """
return 2*x
Functioning in Python
![Page 8: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/8.jpg)
# my own function!
def dbl( x ):
""" returns double its input, x """
return 2*x
Comments
Docstrings
(1) describes overall what the function does, and
(2) explains what the inputs mean/are
They become part of python's built-in help system! With each function be sure to include
one that
They begin with #
keywords
def starts the functionreturn stops it immediately
and sends back the return value
Some of Python's baggage…
Functioning in Python
![Page 9: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/9.jpg)
>>> undo('caf')
>>> undo(undo('caf'))
def undo(s):
""" this "undoes" its string input, s """
return 'de' + s
Functioning in Python
![Page 10: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/10.jpg)
Conditional Statements
ax**2 + bx + c = 0
If b**2 – 4*a*c is equal to 0, then the equation has 1 solution
If b**2 – 4*a*c is less than 0, then the equation has no solution
If b**2 – 4*a*c is greater than 0, then the equation has 2 solutions
![Page 11: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/11.jpg)
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
![Page 12: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/12.jpg)
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…
![Page 13: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/13.jpg)
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…
Name once - use often!
![Page 14: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/14.jpg)
# 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…
Functioning Python…
Indenting indicates
code "blocks"
qroots "block" is
11 lines
Docstrings become part of python's help
system
function defs must be at the
very left
![Page 15: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/15.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
What is demo(-4) ?
def demo(x): return x + f(x)
def g(x): return -1 * x
![Page 16: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/16.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
def g(x): return -1 * x
![Page 17: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/17.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
fx = -4
return 11*g(x) + g(x/2)def g(x): return -1 * x
![Page 18: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/18.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
fx = -4
return 11*g(x) + g(x/2)def g(x): return -1 * x
These are different x's !
![Page 19: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/19.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
f
g
x = -4
return 11*g(-4) + g(-4/2)
x = -4
return -1.0 * x
def g(x): return -1 * x
![Page 20: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/20.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
def g(x): return -1 * x
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
f
g
x = -4
return 11* 4 + g(-4/2)
x = -4
return -1 * -4 4
![Page 21: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/21.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
def g(x): return -1.0 * x
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
fx = -4
return 11* 4 + g(-4/2)
![Page 22: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/22.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
def g(x): return -1 * x
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
f
g
x = -4
return 11* 4 + g(-4/2)
x = -2
return -1 * -2 2
![Page 23: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/23.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
def g(x): return -1.0 * x
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
fx = -4
return 11* 4 + 2
![Page 24: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/24.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
def g(x): return -1.0 * x
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + f(-4)
fx = -4
return 11* 4 + 2 46
![Page 25: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/25.jpg)
How functions work…
def f(x): return 11*g(x) + g(x/2)
def g(x): return -1.0 * x
What is demo(-4) ?
def demo(x): return x + f(x)
demox = -4
return -4 + 46
42
![Page 26: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/26.jpg)
Thinking sequentially
5! = 5 * 4 * 3 * 2 * 1
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
factorial
5! = 120
![Page 27: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/27.jpg)
Recursion == self-reference!
Thinking recursively
5! = 5 * 4 * 3 * 2 * 1
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
factorial
5! = 120
N! = N * (N-1)!
![Page 28: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/28.jpg)
Warning
def fac(N): return N * fac(N-1)
This is legal code!
![Page 29: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/29.jpg)
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...
Warning
![Page 30: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/30.jpg)
def fac(N):
if N <= 1: return 1
Thinking recursively !
Base Case
![Page 31: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/31.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
Base Case
Recursive Step
Thinking recursively !
Human: Base case and 1 step Computer: Everything else
![Page 32: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/32.jpg)
Behind the curtain…def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(1)
Result: 1 The base case is No Problem!
![Page 33: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/33.jpg)
Behind the curtain…def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
![Page 34: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/34.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
Behind the curtain…
![Page 35: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/35.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
Behind the curtain…
![Page 36: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/36.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
3 * fac(2)
Behind the curtain…
![Page 37: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/37.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
3 * fac(2)
2 * fac(1)
Behind the curtain…
![Page 38: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/38.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
3 * fac(2)
2 * fac(1)
1
"The Stack"
Remembers all of the
individual calls to fac
Behind the curtain…
![Page 39: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/39.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
3 * fac(2)
2 * 1
Behind the curtain…
![Page 40: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/40.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
3 * 2
Behind the curtain…
![Page 41: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/41.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * 6
Behind the curtain…
![Page 42: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/42.jpg)
def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * 24
Behind the curtain…
![Page 43: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/43.jpg)
Recursion Mantra
Let recursion do the work for you.
![Page 44: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/44.jpg)
Recursion Mantra
Let recursion do the work for you.Exploit self-similarity
Produce short, elegant codeLess work !
![Page 45: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/45.jpg)
Recursion Mantra
Let recursion do the work for you.
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!
Exploit self-similarity
Produce short, elegant codeLess work !
![Page 46: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/46.jpg)
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
![Page 47: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/47.jpg)
Breaking Up…
is easy to do with Python.
s = "this has 2 t's"
How do we get at the initial character 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 s?
How do we get at ALL the REST of L?
![Page 48: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/48.jpg)
Breaking Up…
is easy to do with Python.
s = "this has 2 t's"
How do we get at the initial character 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 s?
How do we get at ALL the REST of L?
s[0]
![Page 49: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/49.jpg)
Breaking Up…
is easy to do with Python.
s = "this has 2 t's"
How do we get at the initial character 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 s?
How do we get at ALL the REST of L?
s[0]
s[1:]
![Page 50: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/50.jpg)
Breaking Up…
is easy to do with Python.
s = "this has 2 t's"
How do we get at the initial character 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 s?
How do we get at ALL the REST of L?
s[0]
s[1:]
L[0]
![Page 51: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/51.jpg)
Breaking Up…
is easy to do with Python.
s = "this has 2 t's"
How do we get at the initial character 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 s?
How do we get at ALL the REST of L?
s[0]
s[1:]
L[0]
L[1:]
![Page 52: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/52.jpg)
Recursion Examples
def mylen(s):
""" input: any string, s
output: the number of characters in s
"""
![Page 53: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/53.jpg)
Recursion Examples
def mylen(s):
""" input: any string, s
output: the number of characters in s
"""
if s == '':
return
else:
return
![Page 54: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/54.jpg)
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?
![Page 55: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/55.jpg)
mylen(‘eecs')Behind the curtain…
![Page 56: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/56.jpg)
mylen(‘eecs')Behind the curtain…
1 + mylen('ecs')
![Page 57: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/57.jpg)
mylen(‘eecs')Behind the curtain…
1 + mylen('ecs')
1 + mylen('cs')
![Page 58: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/58.jpg)
mylen(‘eecs')Behind the curtain…
1 + mylen('ecs')
1 + mylen('cs')
1 + mylen('s')
![Page 59: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/59.jpg)
mylen(‘eecs')Behind the curtain…
1 + mylen('ecs')
1 + mylen('cs')
1 + mylen('s')
1 + mylen('')
![Page 60: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/60.jpg)
mylen(‘eecs')Behind the curtain…
1 + mylen('ecs')
1 + mylen('cs')
1 + mylen('s')
1 + mylen('')
= 0
![Page 61: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/61.jpg)
Recursion Examples
def mymax(L):
""" input: a NONEMPTY list, L
output: L's maximum element
"""
![Page 62: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/62.jpg)
Recursion Examples
def mymax(L):
""" input: a NONEMPTY list, L
output: L's maximum element
"""
if len(L) == 1:
return
else:
![Page 63: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/63.jpg)
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:] )
else:
return mymax( L[0:1] + L[2:] )
![Page 64: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/64.jpg)
mymax( [1,7,3,42,5] )Behind the curtain…
![Page 65: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/65.jpg)
"Quiz" on recursion
def power(b,p):
Names:
Handle negative values of p, as well.
""" returns b to the p power using recursion, not ** inputs: ints b and p output: a float"""
Want more power?
power(5,2) == 25.0
For example, power(5,-1) == 0.2 (or so)
def sajak(s):
sajak('wheel of fortune') == 6
""" returns the number of vowels in the input string, s"""
![Page 66: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/66.jpg)
def power(b,p): """ inputs: base b and power p (an int)
implements: b**p = b*b**(p-1)
"""
if p == 0:
return
if p > 0:
return
else:
# p < 0
return
![Page 67: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/67.jpg)
def power(b,p): """ 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
return
![Page 68: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/68.jpg)
def power(b,p): """ 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
![Page 69: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/69.jpg)
def power(b,p): """ 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)
![Page 70: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/70.jpg)
behind the curtainpower(2,3)
![Page 71: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/71.jpg)
def sajak(s):
Base case?when there are no letters, there are ZERO vowels
if it is NOT a vowel, the answer is
Rec. step?
Look at the initial character.
if it IS a vowel, the answer is
![Page 72: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/72.jpg)
def sajak(s):
Base case?when there are no letters, there are ZERO vowels
if it is NOT a vowel, the answer is just the number of vowels in the rest of sRec. step?
Look at the initial character.
if it IS a vowel, the answer is 1 + the number of vowels in the rest of s
![Page 73: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/73.jpg)
def sajak(s):
if len(s) == 0:
return 0
else:
Checking for a vowel: Try #1
Base Case
![Page 74: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/74.jpg)
def sajak(s):
if len(s) == 0:
return 0
else:
Checking for a vowel: Try #1
and
or
not
same as in English! but each side has to be a complete boolean value!
Base Case
![Page 75: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/75.jpg)
def sajak(s):
if len(s) == 0:
return 0
else:
Checking for a vowel: Try #1
and
or
not
same as in English! but each side has to be a complete boolean value!
if s[0] == 'a' or s[0] == 'e' or…
Base Case
![Page 76: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/76.jpg)
inChecking for a vowel: Try #2
def sajak(s):
if len(s) == 0:
return 0
else:
Base Case
![Page 77: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/77.jpg)
def sajak(s):
if len(s) == 0:
return 0
else:
if s[0] not in 'aeiou':
return sajak(s[1:])
else:
return 1+sajak(s[1:])
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
Base Case
Rec. Step
![Page 78: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/78.jpg)
sajak('eerier')behind the curtain
![Page 79: EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University](https://reader035.fdocuments.net/reader035/viewer/2022062409/56649f225503460f94c3b787/html5/thumbnails/79.jpg)
Good luck with Homework #1
The key to understanding recursion is to first
understand recursion…- advice from a student