CS 1110: Introduction to Computing Using Python Lecture 16 More Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White]
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
Announcements: A3 • Due : Thursday, March 30 th , 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
Recall: Divide and Conquer Goal : Solve problem P on a piece of data data Idea : Split data into two parts and solve problem data 1 data 2 Solve Problem P Solve Problem P Combine Answer! 3/23/17 More Recursion 4
Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data ! o l l e H # 2. Break into two parts # 3. Combine the result 3/23/17 More Recursion 5
Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data H e l l o ! # 2. Break into two parts left l e H right ! o l # 3. Combine the result 3/23/17 More Recursion 6
Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data left H e l l o ! # 2. Break into two parts ! o l l e right # 3. Combine the result 3/23/17 More Recursion 7
Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data left H e l l o ! # 2. Break into two parts ! o l l e right CORRECT # 3. Combine the result return A: left + right B: right + left C: left D: right 3/23/17 More Recursion 8
Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data left H e l l o ! # 2. Break into two parts ! o l l e right left = reverse(s[0]) right = reverse(s[1:]) # 3. Combine the result return right+left 3/23/17 More Recursion 9
Note: This question was problematic as presented Example: Reversing a String in lecture, so it has been changed a bit. def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" CORRECT # 1. Handle small data A: if s == "": B: if len(s) <= 2: C: if len(s) <= 1: return s return s return s # 2. Break into two parts D: Either A or C left = reverse(s[0]) would work right = reverse(s[1:]) # 3. Combine the result E: A, B, and C return right+left would all work 3/23/17 More Recursion 10
Note: This question was problematic as presented Example: Reversing a String in lecture, so it has been changed a bit. def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data A: if s == "": B: if len(s) <= 2: C: if len(s) <= 1: return s return s return s # 2. Break into two parts CORRECT D: Either A or C left = s[0] would work right = reverse(s[1:]) # 3. Combine the result E: A, B, and C return right+left would all work 3/23/17 More Recursion 11
Example: Reversing a String def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: Base Case return s # 2. Break into two parts left = s[0] right = reverse(s[1:]) Recursive Case # 3. Combine the result return right+left 3/23/17 More Recursion 12
Alternate Implementation def reverse(s): """Returns: reverse of s Does this work? Precondition: s a string""" A: YES CORRECT # 1. Handle small data if len(s) <= 1: return s B: NO # 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
Alternate Implementation def reverse(s): """Returns: reverse of s Does this work? Precondition: s a string""" A: YES # 1. Handle small data if len(s) <= 1: return s B: NO CORRECT # 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
Alternate Implementation def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" reverse(s[:2]) reverse(s[2:]) # 1. Handle small data H e l l o ! if len(s) <= 1: return s reverse(s[:2]) reverse(s[2:]) # 2. Break into two parts H e left = reverse(s[:2]) right = reverse(s[2:]) reverse(s[:2]) reverse(s[2:]) # 3. Combine the result H e Uh oh. Not return right+left proceeding. 3/23/17 More Recursion 15
Alternate Implementation def reverse(s): """Returns: reverse of s Does this work? Precondition: s a string""" # 1. Handle small data A: YES CORRECT if len(s) <= 1: return s B: NO 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
Alternate Implementation def reverse(s): """Returns: reverse of s Does this work? Precondition: s a string""" # 1. Handle small data A: YES CORRECT if len(s) <= 1: return s B: NO # 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
Alternate Implementation H e l l o ! half = 3 reverse(s[half:]) reverse(s[:half]) half = 1 H e l half = 1 l o ! reverse(s[:half]) reverse(s[half:]) reverse(s[:half]) reverse(s[half:]) H e l l o ! half = 1 half = 1 reverse(s[:half]) reverse(s[half:]) reverse(s[:half]) reverse(s[half:]) e l o ! 3/23/17 More Recursion 18
Alternate Implementation ! o l l e H reverse(s[half:]) reverse(s[:half]) l e H ! o l reverse(s[:half]) reverse(s[half:]) reverse(s[:half]) reverse(s[half:]) H l e l o ! reverse(s[:half]) reverse(s[half:]) reverse(s[:half]) reverse(s[half:]) e l o ! 3/23/17 More Recursion 19
Example: Palindromes • Example: AMANAPLANACANALPANAMA • Can we define recursively? 3/23/17 More Recursion 20
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: have to be the same AMANAPLANACANALPANAMA has to be a palindrome • Implement: def ispalindrome(s): """Returns: True if s is a palindrome""" 3/23/17 More Recursion 21
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): Recursive """Returns: True if s is a palindrome""" Definition if len(s) < 2: Base case return True ends = s[0] == s[-1] middle = ispalindrome(s[1:-1]) Recursive case return ends and middle 3/23/17 More Recursion 22
Recursion and Objects • Class Person ( person.py ) Eva Dan Heather Objects have 3 attributes name : String John Sr. Pamela parent1 : Person (or None ) parent2 : Person (or None ) John Jr. Jane Robert Ellen • Represents the “family tree” Goes as far back as known Attributes parent1 and parent2 Alice John III are None if not known • Constructor : Person(name,p1,p2) John IV Or Person(n) if no parents known • 3/23/17 More Recursion 23
Recursion and Objects def num_ancestors(p): Eva Dan Heather """Returns: num of known ancestors Pre: p is a Person""" # 1. Handle small data. John Sr. Pamela # No parent1 or parent2 #(no ancestors) John Jr. Jane Robert Ellen # 2. Break into two parts # Has parent1 or parent2 # Count ancestors of each one Alice John III # (plus parent1, parent2 themselves) John IV 11 ancestors # 3. Combine the result 3/23/17 More Recursion 24
Recursion and Objects def num_ancestors(p): Eva Dan Heather """Returns: num of known ancestors Pre: p is a Person""" John Sr. Pamela # 1. Handle small data. if p.parent1 == None and p.parent2 == None: return 0 # 2. Break into two parts John Jr. Jane Robert Ellen parent1s = 0 if p.parent1 != None: parent1s = 1+num_ancestors(p.parent1s) Alice John III parent2s = 0 if p.parent2 != None: parent2s = 1+num_ancestors(p.parent2s) John IV # 3. Combine the result 11 ancestors return parent1s+parent2s 3/23/17 More Recursion 25
Recommend
More recommend