Lecture 15 More Recursion
Announcements for This Lecture Prelim 1 Assignments and Labs • Prelim 1 back today! • Need to be working on A4 § Access in Gradescope § Instructions are posted § Solution posted in CMS § Just reading it takes a while § Mean : 71, Median : 74 § Slightly longer than A3 § Testing was horrible § Problems are harder • What are letter grades? • Lab Today : lots of practice! § A : 80 (consultant level) § First 4 functions mandatory § B : 60-79 (major level) § Many optional ones too § C : 30-55 (passing) § Exam questions on Prelim 2 10/16/18 More Recursion 2
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! 10/16/18 More Recursion 3
Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle small data if len(s) <= 1: ! o l l e H return s # 2. Break into two parts H e l l o ! # 3. Combine the result ! o l l e H 10/16/18 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 if len(s) <= 1: ! o l l e H return s # 2. Break into two parts H e l l o ! left = s[0] right = reverse(s[1:]) # 3. Combine the result ! o l l e H 10/16/18 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 if len(s) <= 1: ! o l l e H return s # 2. Break into two parts H e l l o ! left = s[0] right = reverse(s[1:]) # 3. Combine the result ! o l l e H return right+left 10/16/18 More Recursion 6
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 10/16/18 More Recursion 7
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 Remove recursive call # 2. Break into two parts left = s[0] right = reverse(s[1:]) Recursive Case # 3. Combine the result return right+left 10/16/18 More Recursion 8
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 5 341267 10/16/18 More Recursion 9
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 5 341267 commafy 341,267 10/16/18 More Recursion 10
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 5 341267 commafy 5 341,267 10/16/18 More Recursion 11
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 5 341267 commafy 5 , 341,267 10/16/18 Always? When? More Recursion 12
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 Approach 2 5 341267 5341 267 commafy 5 , 341,267 10/16/18 Always? When? More Recursion 13
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 Approach 2 5 341267 5341 267 commafy commafy 5 , 341,267 5,341 10/16/18 Always? When? More Recursion 14
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 Approach 2 5 341267 5341 267 commafy commafy 5 , 341,267 5,341 267 10/16/18 Always? When? More Recursion 15
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" Approach 1 Approach 2 5 341267 5341 267 commafy commafy 5 , 341,267 5,341 , 267 10/16/18 Always? When? More Recursion Always! 16
How to Break Up a Recursive Function? def commafy(s): """Returns: string with commas every 3 digits e.g. commafy('5341267') = '5,341,267' Precondition: s represents a non-negative int""" # 1. Handle small data. if len(s) <= 3: Base Case return s # 2. Break into two parts left = commafy(s[:-3]) Recursive right = s[-3:] # Small part on RIGHT Case # 3. Combine the result return left + ',' + right 10/16/18 More Recursion 17
How to Break Up a Recursive Function? def exp(b, c) """Returns: b c Precondition: b a float, c ≥ 0 an int""" Approach 1 Approach 2 12 256 = 12 × (12 255 ) 12 256 = (12 128 ) × (12 128 ) Recursive Recursive Recursive b c = b × (b c-1 ) b c = (b × b) c/2 if c even 10/16/18 More Recursion 18
Raising a Number to an Exponent Approach 1 Approach 2 def exp(b, c) def exp(b, c) """Returns: b c """Returns: b c Precond: b a float, c ≥ 0 an int""" Precond: b a float, c ≥ 0 an int""" # b 0 is 1 # b 0 is 1 if c == 0: if c == 0: return 1 return 1 # b c = b(b c-1 ) # c > 0 left = b if c % 2 == 0: right = exp(b,c-1) return exp(b*b,c//2) return left*right return b*exp(b*b,(c-1)//2) 10/16/18 More Recursion 19
Raising a Number to an Exponent Approach 1 Approach 2 def exp(b, c) def exp(b, c) """Returns: b c """Returns: b c Precond: b a float, c ≥ 0 an int""" Precond: b a float, c ≥ 0 an int""" # b 0 is 1 # b 0 is 1 if c == 0: if c == 0: return 1 return 1 # b c = b(b c-1 ) # c > 0 left right left = b if c % 2 == 0: right = exp(b,c-1) return exp(b*b,c//2) return left*right return b*exp(b*b,(c-1)//2) left right 10/16/18 More Recursion 20
Raising a Number to an Exponent def exp(b, c) c # of calls """Returns: b c 0 0 1 1 Precond: b a float, c ≥ 0 an int""" # b 0 is 1 2 2 4 3 if c == 0: 8 4 return 1 16 5 32 6 # c > 0 2 n n + 1 if c % 2 == 0: return exp(b*b,c//2) 32768 is 215 b 32768 needs only 215 calls! return b*exp(b*b,(c-1)//2) 10/16/18 More Recursion 21
Recursion and Objects • Class Person ( person.py ) ??? Eva Dan Heather § Objects have 3 attributes § name : String John Sr. Pamela ??? ??? § mom : Person (or None ) § dad : Person (or None ) John Jr. Jane Robert Ellen • Represents the “family tree” § Goes as far back as known § Attributes mom and dad John III Alice are None if not known • Constructor : Person(n,m,d) John IV Or Person(n) if no mom, dad • 10/16/18 More Recursion 22
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 mom or dad (no ancestors) # 2. Break into two parts John Jr. Jane Robert Ellen # Has mom or dad # Count ancestors of each one # (plus mom, dad themselves) John III Alice John IV # 3. Combine the result 11 ancestors 10/16/18 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 ??? ??? if p.mom == None and p.dad == None: return 0 # 2. Break into two parts John Jr. Jane Robert Ellen moms = 0 if not p.mom == None: moms = 1+num_ancestors(p.mom) John III Alice dads = 0 if not p.dad== None: dads = 1+num_ancestors(p.dad) John IV # 3. Combine the result 11 ancestors return moms+dads 10/16/18 More Recursion 24
Is All Recursion Divide and Conquer? • Divide and conquer implies two halves “equal” § Performing the same check on each half § With some optimization for small halves • Sometimes we are given a recursive definition § Math formula to compute that is recursive § String definition to check that is recursive § Picture to draw that is recursive § Example : n! = n (n-1)! • In that case, we are just implementing definition 10/16/18 More Recursion 25
Recommend
More recommend