45
Lecture 15: Recursion (Sections 5.85.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] http://www.cs.cornell.edu/courses/cs1110/2020sp

Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Lecture 15:  Recursion

(Sections 5.8‐5.10)

CS 1110Introduction to Computing Using Python

[E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

http://www.cs.cornell.edu/courses/cs1110/2020sp

Page 2: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

• Prelim 1 feedback expected by Sunday• Read §5.8‐5.10 (if you haven’t done so already)

Announcements

2

No-laptop zone on your left

front

ok

Page 3: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

• We know everyone is stressed out  As situation and planning evolve we’ll keep you posted

• Lecture: recording is available.  If you want to avoid lecture room even before break, it’s ok.  You can view recording instead.

• Labs: exercises online. We’re trying to work out ways to provide interactive help somehow.  Will need combination of technologies and platforms

• Office/consulting hours: ditto• How will future exams work? This is difficult to deal with. In 

discussion inside and outside CS to come up with solution.• Please use Piazza!  Good way to get answers to clarification 

questions. 

Thinking about upcoming changes

3

Page 4: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Recursion

Recursive Function: A function that calls itself

(see also Recursive Function)

Two parts to every recursive function:1. A simple case: can be solved easily2. A complex case: can be made simpler (and simpler,

and simpler… until it looks like the simple case)

4

Page 5: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

5

Russian Dolls!

Page 6: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

6

What is the simple case that can be solved easily?

A: The case where the doll has a seam and another doll inside of it.B: The case where the doll has no seam and no doll inside of it.C: A & B are both simpleD: I do not know

Page 7: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

7

Russian Dolls!

import russiand1 = russian.Doll("Dmitry", None)d2 = russian.Doll("Catherine", d1)

id1

name

hasSeam

"Dmitry"

False

None

Doll

innerDoll

id1d1

Heap  Space Global  Space 

id2

name

hasSeam

"Catherine"

True

id1

Doll

innerDoll

id2d2"Dmitry"

"Catherine"

Page 8: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

def open_doll(d):"""Input: a Russian DollOpens the Russian Doll d """print("My name is "+ d.name)if d.hasSeam:

inner = d.innerDollopen_doll(inner)

else:print("That's it!")

idx

namehasSeam

Doll

innerDoll

Page 9: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Examples

• Russian Dolls• Blast Off!• Factorial• Deblank

9

Page 10: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

blast_off(5) # must be a non-negative int

54321BLAST OFF!

blast_off(0)BLAST OFF!

10

Blast Off!

Page 11: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

def blast_off(n):"""Input: a non-negative intCounts down from n to Blast-Off! """if (n == 0):

print("BLAST OFF!")else:

print(n)blast_off(n-1)

12

Blast Off!

Page 12: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

A Mathematical Example: Factorial

• Non-recursive definition:n! = n × n-1 × … × 2 × 1

= n (n-1 × … × 2 × 1)

• Recursive definition:n! = n (n-1)!0! = 1

Recursion 13

for n > 0 Recursive caseBase case

What happens if there is no base case?

Page 13: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Factorial as a Recursive Function

def factorial(n):"""Returns: factorial of n.Pre: n ≥ 0 an int"""if n == 0:

return 1

return n*factorial(n-1)

• n! = n (n-1)!• 0! = 1

Recursion 14

What happens if there is no base case?

Recursive case

Base case(s)

Page 14: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Recursion vs Iteration

• Recursion is provably equivalent to iteration Iteration includes for-loop and while-loop (later) Anything can do in one, can do in the other

• But some things are easier with recursion And some things are easier with iteration

• Will not teach you when to choose recursion That’s for upper level courses

• We just want you to understand the technique15

Page 15: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Recursion is great for Divide and Conquer

Goal: Solve problem P on a piece of data

16

data

Page 16: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Recursion is great for Divide and Conquer

Goal: Solve problem P on a piece of data

17

dataIdea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P

Page 17: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Recursion is great for Divide and Conquer

Goal: Solve problem P on a piece of data

18

dataIdea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P

Combine Answer!

Page 18: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

Count the number of 'e's in a string:

19

p e nn e

Two 'e's

p e nn e

One 'e' One 'e'

Page 19: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

Count the number of 'e's in a string:

20

p e nn e

Two 'e's

p e nn e

Zero 'e's Two 'e's

Page 20: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

Count the number of 'e's in a string:

21

p e nn e

Two 'e's

p e nn e

Zero 'e's Two 'e's

Will talk about how to break-up later

Page 21: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer

Goal: Solve really big problem PIdea: Split into simpler problems, solve, combine

3 Steps:1. Decide what to do for simple cases2. Decide how to break up the task3. Decide how to combine your work

22

Page 22: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Three Steps for Divide and Conquer

1. Decide what to do on “small” data Some data cannot be broken up Have to compute this answer directly

2. Decide how to break up your data Both “halves” should be smaller than whole Often no wrong way to do this (next lecture)

3. Decide how to combine your answers Assume the smaller answers are correct Combining them should give bigger answer

23

Page 23: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

def num_es(s):"""Returns: # of 'e's in s"""# 1. Handle small dataif s == '':

return 0elif len(s) == 1:

return 1 if s[0] == 'e' else 0

# 2. Break into two partsleft = num_es(s[0])right = num_es(s[1:])

# 3. Combine the resultreturn left+right

25

Page 24: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

def num_es(s):"""Returns: # of 'e's in s"""# 1. Handle small dataif s == '':

return 0elif len(s) == 1:

return 1 if s[0] == 'e' else 0

# 2. Break into two partsleft = num_es(s[0])right = num_es(s[1:])

# 3. Combine the resultreturn left+right

27

“Short-cut” forif s[0] == 'e’:

return 1else:

return 0

Page 25: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

def num_es(s):"""Returns: # of 'e's in s"""# 1. Handle small dataif s == '':

return 0elif len(s) == 1:

return 1 if s[0] == 'e' else 0

# 2. Break into two partsleft = num_es(s[0])right = num_es(s[1:])

# 3. Combine the resultreturn left+right

28

p e nn e

0 2

s[0] s[1:]

Page 26: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

def num_es(s):"""Returns: # of 'e's in s"""# 1. Handle small dataif s == '':

return 0elif len(s) == 1:

return 1 if s[0] == 'e' else 0

# 2. Break into two partsleft = num_es(s[0])right = num_es(s[1:])

# 3. Combine the resultreturn left+right

29

p e nn e

0 2+

s[0] s[1:]

Page 27: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Divide and Conquer Example

def num_es(s):"""Returns: # of 'e's in s"""# 1. Handle small dataif s == '':

return 0elif len(s) == 1:

return 1 if s[0] == 'e' else 0

# 2. Break into two partsleft = num_es(s[0])right = num_es(s[1:])

# 3. Combine the resultreturn left+right

30

Base Case

RecursiveCase

Page 28: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Exercise: Remove Blanks from a String

def deblank(s):"""Returns: s but with its blanks removed"""

1. Decide what to do on “small” data If it is the empty string, nothing to do

if s == '':return s

If it is a single character, delete it if a blankif s == ' ': # There is a space here

return '' # Empty stringelse:

return s31

Page 29: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Exercise: Remove Blanks from a String

def deblank(s):"""Returns: s but with its blanks removed"""

2. Decide how to break it upleft = deblank(s[0]) # A string with no blanksright = deblank(s[1:]) # A string with no blanks

3. Decide how to combine the answerreturn left+right # String concatenation

32

Page 30: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Putting it All Together

def deblank(s):"""Returns: s w/o blanks"""if s == '':

return selif len(s) == 1:

return '' if s[0] == ' ' else s

left = deblank(s[0])right = deblank(s[1:])

return left+right

33

Handle small data

Break up the data

Combine answers

Page 31: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Putting it All Together

def deblank(s):"""Returns: s w/o blanks"""if s == '':

return selif len(s) == 1:

return '' if s[0] == ' ' else s

left = deblank(s[0])right = deblank(s[1:])

return left+right

34

Base Case

RecursiveCase

Page 32: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Following the Recursion

a b cdeblank

35

deblank a b cdeblank

stop (base case)adeblank

stop (base case)

b cdeblank

…You really, really, really want to visualize a call of deblank using Python Tutor.  Pay attention to the recursive calls (call frames opening up), the completion of a call (sending the result to the call frame “above”), and the resulting accumulation of the answer.

Page 33: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Breaking it up (1)

a b cdeblank

a b cdeblank

36

Page 34: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Breaking it up (2)

a b c

a

deblank

a b cdeblank

b cdeblank

37

Page 35: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Breaking it up (3)

a b c

a

deblank

a b cdeblank

b cdeblank

b cdeblank

38

Page 36: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Breaking it up (4)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

39

Page 37: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Breaking it up (5)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

40

Page 38: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Breaking it up (6)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c 41

Page 39: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (1)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c c 42

Page 40: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (2)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c c

c

43

Page 41: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (3)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c c

c

cb

44

Page 42: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (4)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c c

c

cb

cb

45

Page 43: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (5)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c c

c

cb

cb

cba

46

Page 44: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (6)

a b c

a

b

deblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

c c

c

cb

cb

cba

cba

47

Page 45: Lecture 15: Recursion - cs.cornell.edu · Lecture 15: Recursion (Sections 5.8‐5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee,

Combining Left+Right (7)

a b c

a

b

c c

c

cb

cb

cba

cba

cbadeblank

a b cdeblank

b cdeblank

b cdeblank

cdeblank

cdeblank

48