Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

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 ... – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 21
Provided by: RonS203
Category:

less

Transcript and Presenter's Notes

Title: Recursion


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 L0 sum(L1)

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 L0 sum(L1
  • sum(4,5,6)
  • 4sum(5,6)
  • 5sum(6)
  • 6sum( )
  • 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)
  • 4sum(5,6)
  • 5sum(6)
  • 6sum( )
  • 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 L0 sum(L1)

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 L0 sum(L1)

L ? L1 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 L0 sum(L1)

11
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)

12
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list

13
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list

14
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list
  • 0 number

15
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list
  • 0 number
  • 5,6 list

16
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 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 \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list
  • 0 number
  • 5,6 list
  • 11 number
  • 411 number

18
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list
  • 0 number
  • 5,6 list
  • 11 number
  • 411 number

?
19
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list
  • 0 number
  • 5,6 list
  • 11 number
  • 411 number

?
?
20
Recursion Worksheet
  • def sum(L)
  • if len(L) 0
  • return 0
  • else
  • return \
  • L0sum(L1)
  • a sum(4,5,6)
  • 4,5,6 list
  •   list
  • 0 number
  • 5,6 list
  • 11 number
  • 411 number

?
?
?
Write a Comment
User Comments (0)
About PowerShow.com