Recursion Announcements for Today Prelim 1 Other Announcements - - PowerPoint PPT Presentation

recursion announcements for today
SMART_READER_LITE
LIVE PREVIEW

Recursion Announcements for Today Prelim 1 Other Announcements - - PowerPoint PPT Presentation

Lecture 15 Recursion Announcements for Today Prelim 1 Other Announcements Reading: 5.8 5.10 Tonight at 7:30-9pm Assignment 3 now graded AJ (Uris G01) Mean 94, Median 99 K-Z (Statler Auditorium) Time


slide-1
SLIDE 1

Recursion

Lecture 15

slide-2
SLIDE 2

Announcements for Today

Prelim 1

  • Tonight at 7:30-9pm

§ A–J (Uris G01) § K-Z (Statler Auditorium)

  • Graded by noon on Sun

§ Scores will be in CMS § In time for drop date

  • Make-ups were e-mailed

§ If not, e-mail Jessica NOW

Other Announcements

  • Reading: 5.8 – 5.10
  • Assignment 3 now graded

§ Mean 94, Median 99 § Time: 7 hrs, StdDev: 3 hrs § Unchanged from last year

  • Assignment 4 posted Friday

§ Parts 1-3: Can do already § Part 4: material from today § Due two weeks from today

10/15/15 Recursion 2

slide-3
SLIDE 3

Recursion

  • Recursive Definition:

A definition that is defined in terms of itself

  • Recursive Function:

A function that calls itself (directly or indirectly)

  • Recursion: If you understand the definition, stop;
  • therwise, see Recursion
  • Infinite Recursion: See Infinite Recursion

10/15/15 Recursion 3

slide-4
SLIDE 4

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

10/15/15 Recursion 4

for n ≥ 0 Recursive case Base case What happens if there is no base case?

slide-5
SLIDE 5

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

10/15/15 Recursion 5

What happens if there is no base case? Recursive case Base case(s)

slide-6
SLIDE 6

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

§ Get the next number by adding previous two § What is a8?

10/15/15 Recursion 6

A: a8 = 21 B: a8 = 29 C: a8 = 34 D: None of these.

slide-7
SLIDE 7

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

§ Get the next number by adding previous two § What is a8?

10/15/15 Recursion 7

A: a8 = 21 B: a8 = 29 C: a8 = 34 D: None of these. correct

slide-8
SLIDE 8

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

§ Get the next number by adding previous two § What is a8?

  • Recursive definition:

§ an = an-1 + an-2 Recursive Case § a0 = 1 Base Case § a1 = 1 (another) Base Case

10/15/15 Recursion 8

Why did we need two base cases this time?

slide-9
SLIDE 9

Fibonacci as a Recursive Function

def fibonacci(n):

"""Returns: Fibonacci no. an Precondition: n ≥ 0 an int""" if n <= 1: return 1 return (fibonacci(n-1)+ fibonacci(n-2))

10/15/15 Recursion 9

Recursive case Base case(s) Note difference with base case conditional.

slide-10
SLIDE 10

Fibonacci as a Recursive Function

def fibonacci(n):

"""Returns: Fibonacci no. an Precondition: n ≥ 0 an int""" if n <= 1: return 1 return (fibonacci(n-1)+ fibonacci(n-2))

  • Function that calls itself

§ Each call is new frame § Frames require memory § ∞ calls = ∞ memory

10/15/15 Recursion 10

n fibonacci 3 5 n fibonacci 1 4 n fibonacci 1 3

slide-11
SLIDE 11

Fibonacci: # of Frames vs. # of Calls

  • Fibonacci is very inefficient.

§ fib(n) has a stack that is always ≤ n § But fib(n) makes a lot of redundant calls

fib(5) fib(4) fib(3) fib(2) fib(2) fib(1) fib(0) fib(0) fib(1) fib(1)

11 Recursion

fib(3) fib(2) fib(1) fib(0) fib(1)

slide-12
SLIDE 12

Fibonacci: # of Frames vs. # of Calls

  • Fibonacci is very inefficient.

§ fib(n) has a stack that is always ≤ n § But fib(n) makes a lot of redundant calls

fib(5) fib(4) fib(3) fib(2) fib(2) fib(1) fib(0) fib(0) fib(1) fib(1)

12 Recursion

fib(3) fib(2) fib(1) fib(0) fib(1) Path to end = the call stack

slide-13
SLIDE 13

Two Major Issues with Recursion

  • How are recursive calls executed?

§ We saw this with the Fibonacci example § Use the call frame model of execution

  • How do we understand a recursive function

(and how do we create one)?

§ You cannot trace the program flow to understand what a recursive function does – too complicated § You need to rely on the function specification

10/15/15 Recursion 13

slide-14
SLIDE 14

How to Think About Recursive Functions

  • 1. Have a precise function specification.
  • 2. Base case(s):

§ When the parameter values are as small as possible § When the answer is determined with little calculation.

  • 3. Recursive case(s):

§ Recursive calls are used. § Verify recursive cases with the specification

  • 4. Termination:

§ Arguments of calls must somehow get “smaller” § Each recursive call must get closer to a base case

10/15/15 Recursion 14

slide-15
SLIDE 15

Understanding the String Example

def num_es(s): """Returns: # of 'e's in s""" # s is empty if s == '': return 0 # s has at least one 'e' if s[0] == 'e': return 1+num_es(s[1:]) return num_es(s[1:]))

  • Break problem into parts
  • Solve small part directly

s 1 len(s)

H ello World! Recursive case Base case

number of e’s in s = number of e’s in s[0] + number of e’s in s[1:] number of e’s in s = number of e’s in s[1:] (+1 if s[0] is an 'e') (+0 is s[0] not an 'e')

10/15/15 Recursion 15

slide-16
SLIDE 16

Understanding the String Example

  • Step 1: Have a precise specification

def num_es(s): """Returns: # of 'e's in s""" # s is empty if s == '': return 0 # return # of 'e's in s[0]+# of 'e's in s[1:]

if s[0] == 'e': return 1+num_es(s[1:]) return num_es(s[1:]))

  • Step 2: Check the base case

§ When s is the empty string, 0 is (correctly) returned.

Recursive case Base case

“Write” your return statement using the specification

10/15/15 Recursion 16

slide-17
SLIDE 17

Understanding the String Example

  • Step 3: Recursive calls make progress toward termination

def num_es(s): """Returns: # of 'e's in s""" # s is empty if s == '': return 0 # return # of 'e's in s[0]+# of 'e's in s[1:]

if s[0] == 'e': return 1+num_es(s[1:]) return num_es(s[1:]))

  • Step 4: Check the recursive case

§ Does it match the specification?

argument s[1:] parameter s argument s[1:] is smaller than parameter s, so there is progress toward reaching base case 0

10/15/15 Recursion 17

slide-18
SLIDE 18

Exercise: Remove Blanks from a String

  • 1. Have a precise specification

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

  • 2. Base Case: the smallest String s is ''.

if s == '': return s

  • 3. Other Cases: String s has at least 1 character.

return (s[0] with blanks removed) + (s[1:] with blanks removed)

10/15/15 Recursion 18

slide-19
SLIDE 19

Exercise: Remove Blanks from a String

  • 1. Have a precise specification

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

  • 2. Base Case: the smallest String s is ''.

if s == '': return s

  • 3. Other Cases: String s has at least 1 character.

return (s[0] with blanks removed) + (s[1:] with blanks removed)

10/15/15 Recursion 19

('' if s[0] == ' ' else s[0])

slide-20
SLIDE 20

What the Recursion Does

a b c deblank

10/15/15 Recursion 20

slide-21
SLIDE 21

What the Recursion Does

a b c deblank a b c deblank

10/15/15 Recursion 21

slide-22
SLIDE 22

What the Recursion Does

a b c a deblank a b c deblank b c deblank

10/15/15 Recursion 22

slide-23
SLIDE 23

What the Recursion Does

a b c a deblank a b c deblank b c deblank b c deblank

10/15/15 Recursion 23

slide-24
SLIDE 24

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank

10/15/15 Recursion 24

slide-25
SLIDE 25

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 25

slide-26
SLIDE 26

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 26

c

slide-27
SLIDE 27

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 27

c c

slide-28
SLIDE 28

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 28

c c c

slide-29
SLIDE 29

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 29

c c c

c b

slide-30
SLIDE 30

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 30

c c c

c b c b

slide-31
SLIDE 31

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 31

c c c

c b c b

c b a

slide-32
SLIDE 32

What the Recursion Does

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

10/15/15 Recursion 32

c c c

c b c b

c b a c b a

slide-33
SLIDE 33

What the Recursion Does

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

10/15/15 Recursion 33

slide-34
SLIDE 34

Exercise: Remove Blanks from a String

def deblank(s): """Returns: s with blanks removed""" if s == '': return s # s is not empty if s[0] is a blank: return s[1:] with blanks removed # s not empty and s[0] not blank return (s[0] + s[1:] with blanks removed)

  • Sometimes easier to break

up the recursive case

§ Particularly om small part § Write recursive case as a sequence of if-statements

  • Write code in pseudocode

§ Mixture of English and code § Similar to top-down design

  • Stuff in red looks like the

function specification!

§ But on a smaller string § Replace with deblank(s[1:])

10/15/15 Recursion 34

slide-35
SLIDE 35

Exercise: Remove Blanks from a String

def deblank(s): """Returns: s with blanks removed""" if s == '': return s # s is not empty if s[0] in string.whitespace: return deblank(s[1:]) # s not empty and s[0] not blank return (s[0] + deblank(s[1:]))

  • Check the four points:
  • 1. Precise specification?
  • 2. Base case: correct?
  • 3. Progress towards

termination?

  • 4. Recursive case: correct?

10/15/15 Recursion 35

Module string has special constants to simplify detection of whitespace and other characters.

slide-36
SLIDE 36

Example: Reversing a String

  • Precise Specification:

§ Returns: reverse of s

  • Solving with recursion

§ Suppose we can reverse a smaller string (e.g. less one character) § Can we use that solution to reverse whole string?

  • Often easy to understand

first without Python

§ Then sit down and code

10/15/15 Recursion 36

H e l l

  • !

!

  • l

l e H e l l

  • !

H

slide-37
SLIDE 37

Example: Reversing a String

  • Precise Specification:

§ Returns: reverse of s

  • Solving with recursion

§ Suppose we can reverse a smaller string (e.g. less one character) § Can we use that solution to reverse whole string?

  • Often easy to understand

first without Python

§ Then sit down and code

10/15/15 Recursion 37

H e l l

  • !

!

  • l

l e H H e l l

  • !
slide-38
SLIDE 38

Example: Reversing a String

  • Precise Specification:

§ Returns: reverse of s

  • Solving with recursion

§ Suppose we can reverse a smaller string (e.g. less one character) § Can we use that solution to reverse whole string?

  • Often easy to understand

first without Python

§ Then sit down and code

10/15/15 Recursion 38

H e l l

  • !

!

  • l

l e H e l l

  • !

H !

  • l

l e

slide-39
SLIDE 39

Example: Reversing a String

  • Precise Specification:

§ Returns: reverse of s

  • Solving with recursion

§ Suppose we can reverse a smaller string (e.g. less one character) § Can we use that solution to reverse whole string?

  • Often easy to understand

first without Python

§ Then sit down and code

10/15/15 Recursion 39

H e l l

  • !

!

  • l

l e H e l l

  • !

!

  • l

l e H

slide-40
SLIDE 40

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # s is empty if s == '': return s # s has at least one char # (reverse of s[1:])+s[0] return reverse(s[1:])+s[0]

10/15/15 Recursion 40

e l l

  • !

!

  • l

l e H

  • 1. Precise specification?
  • 2. Base case: correct?
  • 3. Recursive case:

progress to termination?

  • 4. Recursive case: correct?

✔ ✔ ✔ ✔

slide-41
SLIDE 41

Next Time: Recursion vs. For-Loops

10/15/15 Recursion 41