Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:

Similar presentations


Presentation on theme: "Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:"— Presentation transcript:

1 Recursion

2 Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:])

3 How iteration works def sum(L): total = 0 for i in L: total += i return total sum([4,5,6]) total = 0 i = 4 total = 0 + 4 = 4 i = 5 total = 4 + 5 = 9 i = 6 total = 9 + 6 = 15 15

4 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

5 Side by side sum([4,5,6]) total = 0 i = 4 total = 0 + 4 = 4 i = 5 total = 4 + 5 = 9 i = 6 total = 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

6 Fact Iteration and recursion are equivalent. Any iteration can be replaced by recursion. Any recursion can be replaced by iteration.

7 Why use Recursion? Clarity: The logic of certain task can sometimes be clarified. Elegance: The coding is sometimes more elegant.

8 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 [ ] is the base case

9 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 [ ]

10 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:])

11 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:

12 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

13 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

14 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

15 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

16 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

17 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

18 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 

19 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  

20 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   


Download ppt "Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:"

Similar presentations


Ads by Google