Lecture 15: Recursion
(Sections 5.8-5.10)
CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 15: Recursion (Sections 5.8-5.10) CS 1110 Introduction to - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 15: Recursion (Sections 5.8-5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements Prelim 1
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
3
4
5
4
left middle right
3 2 1
6
1
left middle right
7
left middle right
2 1
8
left middle right
2 1
9
left middle right
2 1
10
left middle right
3 2 1
11
left middle right
3 2 1
12
left middle right
3 2 1
13
left middle right
3 2 1
14
left middle right
3 2 1
15
left middle right
3 2 1
16
left middle right
3 2 1
4
17
left middle right
3 2 1
18
19
1
left middle right
20
4
left
3 2 1
21
4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1
1 2 3
4
22
left middle right
3 2 1
1 2 3
23
if (n== 1): print(“move from ”+source+“ to ”+target) else: # need to move top n_disks-1 disks from source to # other… luckily, I have a function that does that! solve_hanoi(source, other, target, n_disks-1) # move the last disk to the target, that’s easy! print(“move from ”+source+“ to ”+target) # now put everything back on the last disk at target solve_hanoi(other, target, source, n_disks-1)
24
25
26
27
Solve Problem P Solve Problem P
28
Solve Problem P Solve Problem P Combine Answer!
def deblank(s): """Returns: s but with its blanks removed"""
if s == '': return s
if s == ' ': # There is a space here return '' # Empty string else: return s
29
def deblank(s): """Returns: s but with its blanks removed"""
30
def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: if s[0] == ‘ ‘: return '' else: return s left = deblank(s[0]) right = deblank(s[1:]) return left+right
31
Handle simple cases Break up the data Combine answers
def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: if s[0] == ‘ ‘: return '' else: return s left = deblank(s[0]) right = deblank(s[1:]) return left+right
32
a b c deblank
33
deblank a b c deblank stop (base case) a deblank stop (base case) b c deblank …
a b c deblank a b c deblank
34
a b c a deblank a b c deblank b c deblank
35
a b c a deblank a b c deblank b c deblank b c deblank
36
a b c a b deblank a b c deblank b c deblank b c deblank c deblank
37
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank
38
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c
39
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c
40
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c
41
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b
42
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b
43
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b c b a
44
a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b c b a c b a
45
a b c a b c c c c b c b c b a c b a c b a deblank a b c deblank b c deblank b c deblank c deblank c deblank
46