recursion. sum a list of numbers iterative def sum(l): total = 0 for i in l: total += i return total...
Post on 22-Dec-2015
221 views
TRANSCRIPT
Recursion
Sum a list of numbers
Iterativedef sum(L): total = 0 for i in L: total += ireturn total
Recursivedef sum(L): if len(L) == 0: return 0 else: return L[0]+
sum(L[1:])
How iteration works
def sum(L): total = 0 for i in L: total += ireturn total
• sum([4,5,6]) total = 0i = 4total = 0 + 4 = 4i = 5total = 4 + 5 = 9i = 6total = 9 + 6 =
15• 15
How recursion works
def sum(L): if len(L) == 0: return 0 else: return L[0]+
sum(L[1:]
•sum([4,5,6]) •4+sum([5,6]) •5+sum([6])
•6+sum([ ]) •0 •6 + 0 = 6 •5 + 6 = 11 •4 + 11 = 15•15
Side by side
sum([4,5,6]) total = 0i = 4total = 0 + 4 = 4i = 5total = 4 + 5 = 9i = 6total = 9 + 6 =
15• 15
•sum([4,5,6]) •4+sum([5,6]) •5+sum([6])
•6+sum([ ]) •0 •6 + 0 = 6 •5 + 6 = 11 •4 + 11 = 15•15
Fact
• Iteration and recursion are equivalent.Any iteration can be replaced by recursion.Any recursion can be replaced by iteration.
Why use Recursion?
• Clarity: The logic of certain task can sometimes be clarified.
• Elegance: The coding is sometimes more elegant.
A recursive algorithm must…
…have a base case.
def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:])
Empty list [ ] isthe base case
A recursive algorithm must…
…have a base case.
…change its state and move toward the base case.
def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:])
L L[1:]
Closer to [ ]
A recursive algorithm must…
…have a base case.
…change its state and move toward the base case.
…call itself, recursively.
def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:])
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
[5,6] list
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
[5,6] list 11 number
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
[5,6] list 11 number
4+11 number
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
[5,6] list 11 number
4+11 number
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
[5,6] list 11 number
4+11 number
Recursion Worksheet
def sum(L): if len(L) == 0: return 0 else: return \
L[0]+sum(L[1:])
a = sum([4,5,6])
Function In:Base In: Base Out:Recursion In: Recursion Out:Function Out:
Value Type
1. Input simplified:2. Input types match:3. Output types match:
[4,5,6] list[ ] list 0 number
[5,6] list 11 number
4+11 number