More Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] - - PowerPoint PPT Presentation

more recursion
SMART_READER_LITE
LIVE PREVIEW

More Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] - - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 16 More Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements We cant check off labs in professor office hours Reading for next week: Chapters 15 and 16


slide-1
SLIDE 1

More Recursion

Lecture 16

CS 1110:

Introduction to Computing Using Python

[Andersen, Gries, Lee, Marschner, Van Loan, White]

slide-2
SLIDE 2

Announcements

  • We can’t check off labs in professor office hours
  • Reading for next week: Chapters 15 and 16

3/23/17 More Recursion 2

slide-3
SLIDE 3

Announcements: A3

  • Due: Thursday, March 30th, 11:59pm
  • trigram_generation: “REQUIREMNET [sic]:

first, randomly pick a starting bigram "w1 w2”.”

  • This means, “pick “w1 w2” randomly from the

sample text, just like you picked a unigram from the text in bigram_generation.

3/23/17 More Recursion 3

slide-4
SLIDE 4

Recall: Divide and Conquer

Goal: Solve problem P on a piece of data

3/23/17 More Recursion 4

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P Combine Answer!

slide-5
SLIDE 5

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts # 3. Combine the result

3/23/17 More Recursion 5

H e l l

  • !

!

  • l

l e H

slide-6
SLIDE 6

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts # 3. Combine the result

3/23/17 More Recursion 6

H e l l

  • !

H e l l

  • !

left right

!

  • l

l e H

slide-7
SLIDE 7

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts # 3. Combine the result

3/23/17 More Recursion 7

H e l l

  • !

left right

H e l l

  • !

!

  • l

l e

slide-8
SLIDE 8

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts # 3. Combine the result

return

3/23/17 More Recursion 8

H e l l

  • !

left right

H e l l

  • !

!

  • l

l e

A: left + right B: right + left C: left D: right

CORRECT

slide-9
SLIDE 9

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts left = reverse(s[0]) right = reverse(s[1:]) # 3. Combine the result return right+left

3/23/17 More Recursion 9

H e l l

  • !

left right

H e l l

  • !

!

  • l

l e

slide-10
SLIDE 10

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts left = reverse(s[0]) right = reverse(s[1:]) # 3. Combine the result return right+left

3/23/17 More Recursion 10

H e l l

  • !

A: if s == "": return s B: if len(s) <= 2: return s C: if len(s) <= 1: return s CORRECT E: A, B, and C would all work D: Either A or C would work

Note: This question was problematic as presented in lecture, so it has been changed a bit.

slide-11
SLIDE 11

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data # 2. Break into two parts left = s[0] right = reverse(s[1:]) # 3. Combine the result return right+left

3/23/17 More Recursion 11

H e l l

  • !

A: if s == "": return s B: if len(s) <= 2: return s C: if len(s) <= 1: return s D: Either A or C would work CORRECT E: A, B, and C would all work

Note: This question was problematic as presented in lecture, so it has been changed a bit.

slide-12
SLIDE 12

Example: Reversing a String

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: return s # 2. Break into two parts left = s[0] right = reverse(s[1:]) # 3. Combine the result return right+left

3/23/17 More Recursion 12

Base Case Recursive Case

slide-13
SLIDE 13

Alternate Implementation

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: return s # 2. Break into two parts left = reverse(s[:len(s)-1]) right = reverse(s[len(s)-1]) # 3. Combine the result return right+left

3/23/17 More Recursion 13

A: YES B: NO

Does this work?

CORRECT

slide-14
SLIDE 14

Alternate Implementation

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: return s # 2. Break into two parts left = reverse(s[:2]) right = reverse(s[2:]) # 3. Combine the result return right+left

3/23/17 More Recursion 14

A: YES B: NO

Does this work?

CORRECT

slide-15
SLIDE 15

Alternate Implementation

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: return s # 2. Break into two parts left = reverse(s[:2]) right = reverse(s[2:]) # 3. Combine the result return right+left

3/23/17 More Recursion 15

H e l l

  • !

H e l l

  • !

H e reverse(s[:2]) reverse(s[2:])

reverse(s[:2])

H e

reverse(s[2:]) reverse(s[:2]) reverse(s[2:])

Uh oh. Not proceeding.

slide-16
SLIDE 16

Alternate Implementation

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: return s if len(s) == 2: return s[1] + s[0] # 2. Break into two parts left = reverse(s[:2]) right = reverse(s[2:]) # 3. Combine the result return right+left

3/23/17 More Recursion 16

A: YES B: NO

Does this work?

CORRECT

slide-17
SLIDE 17

Alternate Implementation

def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: return s # 2. Break into two parts half = len(s)/2 left = reverse(s[:half]) right = reverse(s[half:]) # 3. Combine the result return right+left

3/23/17 More Recursion 17

A: YES B: NO

Does this work?

CORRECT

slide-18
SLIDE 18

Alternate Implementation

3/23/17 More Recursion 18

H e l l

  • !

H e l l

  • !

H e

reverse(s[:half]) reverse(s[half:]) reverse(s[:half]) reverse(s[half:])

l

reverse(s[:half]) reverse(s[half:])

e l l

  • reverse(s[:half]) reverse(s[half:])

!

reverse(s[:half]) reverse(s[half:])

  • !

half = 3 half = 1 half = 1 half = 1 half = 1

slide-19
SLIDE 19

Alternate Implementation

3/23/17 More Recursion 19

!

  • l

l e H l e H !

  • l

H l

reverse(s[:half]) reverse(s[half:]) reverse(s[:half]) reverse(s[half:])

e

reverse(s[:half]) reverse(s[half:])

e l l

  • reverse(s[:half]) reverse(s[half:])

!

reverse(s[:half]) reverse(s[half:])

  • !
slide-20
SLIDE 20
  • Example:

AMANAPLANACANALPANAMA

  • Can we define recursively?

Example: Palindromes

3/23/17 20 More Recursion

slide-21
SLIDE 21

have to be the same

Example: Palindromes

  • String with ≥ 2 characters is a palindrome if:
  • its first and last characters are equal, and
  • the rest of the characters form a palindrome
  • Example:

AMANAPLANACANALPANAMA

  • Implement: def ispalindrome(s):

"""Returns: True if s is a palindrome"""

has to be a palindrome

3/23/17 21 More Recursion

slide-22
SLIDE 22

Example: Palindromes

  • String with ≥ 2 characters is a palindrome if:
  • its first and last characters are equal, and
  • the rest of the characters form a palindrome

def ispalindrome(s): """Returns: True if s is a palindrome""" if len(s) < 2: return True ends = s[0] == s[-1] middle = ispalindrome(s[1:-1]) return ends and middle

Recursive case Base case Recursive Definition

3/23/17 22 More Recursion

slide-23
SLIDE 23

Recursion and Objects

  • Class Person (person.py)
  • Objects have 3 attributes
  • name: String
  • parent1: Person (or None)
  • parent2: Person (or None)
  • Represents the “family tree”
  • Goes as far back as known
  • Attributes parent1 and parent2

are None if not known

  • Constructor: Person(name,p1,p2)
  • Or Person(n) if no parents known

3/23/17 More Recursion 23

John Sr. Pamela Eva Dan Heather John Jr. Jane Robert Ellen John III Alice John IV

slide-24
SLIDE 24

Recursion and Objects

def num_ancestors(p): """Returns: num of known ancestors Pre: p is a Person""" # 1. Handle small data. # No parent1 or parent2 #(no ancestors) # 2. Break into two parts # Has parent1 or parent2 # Count ancestors of each one # (plus parent1, parent2 themselves) # 3. Combine the result

3/23/17 More Recursion 24

John Sr. Pamela Eva Dan Heather John Jr. Jane Robert Ellen John III Alice John IV

11 ancestors

slide-25
SLIDE 25

Recursion and Objects

def num_ancestors(p): """Returns: num of known ancestors Pre: p is a Person""" # 1. Handle small data. if p.parent1 == None and p.parent2 == None: return 0 # 2. Break into two parts parent1s = 0 if p.parent1 != None: parent1s = 1+num_ancestors(p.parent1s) parent2s = 0 if p.parent2 != None: parent2s = 1+num_ancestors(p.parent2s) # 3. Combine the result return parent1s+parent2s

3/23/17 More Recursion 25

John Sr. Pamela Eva Dan Heather John Jr. Jane Robert Ellen John III Alice John IV

11 ancestors

slide-26
SLIDE 26

def num_ancestors(p): """Returns: num of known ancestors Pre: p is a Person""" # 1. Handle small data. if p.parent1 == None and p.parent2 == None: return 0 # 2. Break into two parts parent1s = 0 if p.parent1 != None: parent1s = 1+num_ancestors(p.parent1s) parent2s = 0 if p.parent2 != None: parent2s = 1+num_ancestors(p.parent2s) # 3. Combine the result return parent1s+parent2s

Recursion and Objects

3/23/17 More Recursion 26

We don’t actually need this. It is handled by the conditionals in #2.

slide-27
SLIDE 27

Challenge: All Ancestors

def all_ancestors(p):

"""Returns: list of all ancestors of p""" # 1. Handle small data. # 2. Break into parts. # 3. Combine answer.

3/23/17 More Recursion 27

John Sr. Pamela Eva Dan Heather John Jr. Jane Robert Ellen John III Alice John IV