Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition...
Transcript of Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition...
![Page 1: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/1.jpg)
Recursion!
![Page 2: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/2.jpg)
What Happens Inside a Function?
def h(x):if x%2 == 1:
return f(x) + xelse:
return f(f(x))def f(x): x = x-1
return g(x)+1
def g(x):return x*2
h(3):return f(3) + 3
f(3): return g(2) + 1
g(2): return 4
4
8
5
Two key points…• Functions return to where they were called from• Each function keeps its own values of its variables
![Page 3: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/3.jpg)
Recursion…
n! = n(n-1)(n-2)…1
def factorial(n): result = 1 for k in range(1, n+1): result = result * k return result
![Page 4: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/4.jpg)
Recursion…
n! = n(n-1)(n-2)…1n! = n[(n-1)!] “inductive definition”
![Page 5: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/5.jpg)
Recursion…
n! = n(n-1)(n-2)…1n! = n[(n-1)!] “inductive definition”0! = 1 “base case”
![Page 6: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/6.jpg)
Math Induction = CS Recursion
0! = 1n! = n[(n-1)!]
Mathinductive definition
Python (Functional)
recursive function
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n * factorial(n-1)
![Page 7: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/7.jpg)
Is Recursion Magic?
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n*factorial(n-1)
factorial(3): return 3 * factorial(2)
![Page 8: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/8.jpg)
Is Recursion Magic?
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n*factorial(n-1)
factorial(3): return 3 * factorial(2)
return 2 * factorial(1)
![Page 9: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/9.jpg)
Is Recursion Magic?
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n*factorial(n-1)
factorial(3): return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
![Page 10: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/10.jpg)
Is Recursion Magic?
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n*factorial(n-1)
factorial(3): return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
1
![Page 11: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/11.jpg)
Is Recursion Magic?
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n*factorial(n-1)
factorial(3): return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
1
2
![Page 12: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/12.jpg)
Is Recursion Magic?
# recursive factorialdef factorial(n): if n == 0: return 1 else: return n*factorial(n-1)
factorial(3): return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
1
2
6
![Page 13: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/13.jpg)
The Power of Recursion…
>>> pow(10, 3)1000
>>> pow(10, 0)1
>>> pow(2, 5)32
def pow(base, exp):
![Page 14: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/14.jpg)
The Power of Recursion…
>>> pow(10, 3)1000
>>> pow(10, 0)1
>>> pow(2, 5)32
def pow(base, exp): if exp == 0: return 1 # base case! else:
![Page 15: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/15.jpg)
The Power of Recursion…
>>> pow(10, 3)1000
>>> pow(10, 0)1
>>> pow(2, 5)32
def pow(base, exp): if exp == 0: return 1 # base case! else: return base * pow(base, exp-1)
![Page 16: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/16.jpg)
Computing the length of a list
>>> len([1, 42, “spam”])3>>> len([1, [2, [3, 4]]])2
def len(L): ‘’’returns the length of list L’’’
Python has this built-in!
![Page 17: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/17.jpg)
Computing the length of a list
>>> len([1, 42, “spam”])3>>> len([1, [2, [3, 4]]])2
def len(L): ‘’’returns the length of list L’’’ if L == []: return 0 # base case
Python has this built-in!
![Page 18: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/18.jpg)
Computing the length of a list
>>> len([1, 42, “spam”])3>>> len([1, [2, [3, 4]]])2
def len(L): ‘’’returns the length of list L’’’ if L == []: return 0 # base case else: return 1 + len(L[1:])
![Page 19: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/19.jpg)
Summing up the numbers in a list
>>> sum([1, 42, 7])50>>> sum([42])42>>> sum([])0
def sum(L): ‘’’returns the sum of numbers in the list’’’
Python has this built-in too!
![Page 20: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/20.jpg)
Summing up the numbers in a list
>>> sum([1, 42, 7])50>>> sum([42])42>>> sum([])0
def sum(L): ‘’’returns the sum of numbers in the list’’’ if L == []: return 0
The base case takes care of the simplest case we could get.
![Page 21: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/21.jpg)
Summing up the numbers in a list
>>> sum([1, 42, 7])50>>> sum([42])42>>> sum([])0
def sum(L): ‘’’returns the sum of numbers in the list’’’ if L == []: return 0 else: return ???
![Page 22: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/22.jpg)
Summing up the numbers in a list
>>> sum([1, 42, 7])50>>> sum([42])42>>> sum([])0
def sum(L): ‘’’returns the sum of numbers in the list’’’ if L == []: return 0 else: return L[0] + sum(L[1:])
![Page 23: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/23.jpg)
No new variables!
def len(L): ‘’’ RECURSIVE VERSION ’’’ if L == []: return 0 # base case else: return 1 + len(L[1:])
def lenV2(L): ‘’’ LOOP VERSION ‘’’ counter = 0 # a new variable! for x in L: counter += 1 return counter
![Page 24: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/24.jpg)
Reversing a list
>>> reverse([1, 2, 3, 4])[4, 3, 2, 1]
def reverse(L):
[ ], ,
![Page 25: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/25.jpg)
Reversing a list
>>> reverse([1, 2, 3, 4])[4, 3, 2, 1]
def reverse(L): if L == []: return ??? # base case
[ ], ,
![Page 26: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/26.jpg)
Reversing a list
>>> reverse([1, 2, 3, 4])[4, 3, 2, 1]
def reverse(L): if L == []: return [] # base case
[ ], ,
![Page 27: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/27.jpg)
Reversing a list
>>> reverse([1, 2, 3, 4])[4, 3, 2, 1]
def reverse(L): if L == []: return [] # base case else: return ???
[ ], ,
We will generally avoid using append when writing recursive functions.
![Page 28: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/28.jpg)
Reversing a list
>>> reverse([1, 2, 3, 4])[4, 3, 2, 1]
def reverse(L): if L == []: return [] # base case else: return reverse(L[1:]) + [L[0]]
[ ], ,
![Page 29: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/29.jpg)
Minimum!>>> min([372, 112, 42, 451])42>>> min([16])16
def min(L): ‘’’ returns smallest value in the list L ‘’’ if ??? # base case?
[ ], ,
Assume that the list always has at least one number in it! Use len as a helper function!
![Page 30: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/30.jpg)
Minimum!>>> min([372, 112, 42, 451])42>>> min([16])16
def min(L): ‘’’ returns smallest value in the list L ‘’’ if len(L) == 1: return L[0] # base case!
elif L[0] < min(L[1:]): ???return L[0] else: return min(L[1:]
[ ], ,
Assume that the list always has at least one number in it! Use len as a helper function!
![Page 31: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/31.jpg)
Minimum!>>> min([372, 112, 42, 451])42>>> min([16])16
def min(L): ‘’’ returns smallest value in the list L ‘’’ if len(L) == 1: return L[0] # base case?
elif L[0] < min(L[1:]): return L[0] else: return ???
[ ], ,
Assume that the list always has at least one number in it! Use len as a helper function!
![Page 32: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/32.jpg)
Minimum!>>> min([372, 112, 42, 451])42>>> min([16])16
def min(L): ‘’’ returns smallest value in the list L ‘’’ if len(L) == 1: return L[0] # base case?
elif L[0] < min(L[1:]): return L[0] else: return min(L[1:])
[ ], ,
Assume that the list always has at least one number in it! Use len as a helper function!
![Page 33: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/33.jpg)
member>>> member(42, [1, 3, 5, 42, 7])True>>> member(42, [“spam”, “is”, “yummy”])False
def member(thing, myList): ‘’’ return True if thing in myList and False otherwise. ‘’’
This is sort of like the “in” thing in Python, but don’t use “in” here. Just list indexing, slicing, and recursion!
![Page 34: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/34.jpg)
member>>> member(42, [1, 3, 5, 42, 7])True>>> member(42, [“spam”, “is”, “yummy”])False
def member(thing, myList): if myList == []: return ??? # base case
![Page 35: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/35.jpg)
member>>> member(42, [1, 3, 5, 42, 7])True>>> member(42, [“spam”, “is”, “yummy”])False
def member(thing, myList): if myList == []: return False # base
case elif thing == myList[0]: return True
![Page 36: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/36.jpg)
member>>> member(42, [1, 3, 5, 42, 7])True>>> member(42, [“spam”, “is”, “yummy”])False
def member(thing, myList): if myList == []: return False # base
case elif thing == myList[0]: return True
else: return member(thing, myList[1:])
![Page 37: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/37.jpg)
Palindrome?>>> pal(“radar”)True>>> pal(“amanaplanacanalpanama”)True>>> pal(“spam”)False
def pal(myString):‘’’ returns True if myString is a palindrome and False otherwise ‘’’
![Page 38: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/38.jpg)
Palindrome?>>> pal(“radar”)True>>> pal(“amanaplanacanalpanama”)True>>> pal(“spam”)False
def pal(myString):‘’’ returns True if myString is a
palindrome and False otherwise ‘’’if len(myString) <= 1: return True # base
case
![Page 39: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/39.jpg)
Palindrome?>>> pal(“radar”)True>>> pal(“amanaplanacanalpanama”)True>>> pal(“spam”)False
def pal(myString):‘’’ returns True if myString is a
palindrome and False otherwise ‘’’if len(myString) <= 1: return True # base
caseelif myString[0] != myString[-1]:
return False else:
![Page 40: Recursion! - unibo.itMath Induction = CS Recursion 0! = 1 n! = n [(n-1)!] Math inductive definition Python (Functional) recursive function # recursive factorial def factorial(n): if](https://reader030.fdocuments.net/reader030/viewer/2022040822/5e6bdb6a4614c35baf73a5e6/html5/thumbnails/40.jpg)
Palindrome?>>> pal(“radar”)True>>> pal(“amanaplanacanalpanama”)True>>> pal(“spam”)False
def pal(myString):‘’’ returns True if myString is a
palindrome and False otherwise ‘’’if len(myString) <= 1: return True # base
caseelif myString[0] != myString[-1]:
return False else: return pal(myString[1:-1])