more recursion
play

More Recursion 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - PowerPoint PPT Presentation

Lecture 15 More Recursion 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Announcements for This Lecture 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Prelim 1 0 0 0


  1. Lecture 15 More Recursion

  2. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Announcements for This Lecture 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Prelim 1 0 0 0 Assignments and Labs 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 • Prelim 1 back today! 0 • Need to be working on A4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § Access in Gradescope § Instructions are posted 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § Solution posted in CMS § Just reading it takes a while 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § Mean : 71, Median : 74 § Slightly longer than A3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § Spent too long on slicing § Problems are harder 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 • What are letter grades? 0 • Lab Today : lots of practice! 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § A : 80 (consultant level) 0 § First 4 functions mandatory 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § B : 60-79 (major level) 0 § Many optional ones too 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 § C : 35-55 (passing) § Exam questions on Prelim 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10/22/19 More Recursion 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  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! 10/22/19 More Recursion 3

  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 ! # 3. Combine the result ! o l l e H 10/22/19 More Recursion 4

  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 10/22/19 More Recursion 5

  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 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/22/19 More Recursion 6

  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 # 2. Break into two parts left = s[0] right = reverse(s[1:]) Recursive Case # 3. Combine the result return right+left 10/22/19 More Recursion 7

  8. 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/22/19 More Recursion 8

  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 10/22/19 More Recursion 9

  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 341,267 10/22/19 More Recursion 10

  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/22/19 More Recursion 11

  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 5 341267 commafy 5 , 341,267 10/22/19 Always? When? More Recursion 12

  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 5 , 341,267 10/22/19 Always? When? More Recursion 13

  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 10/22/19 Always? When? More Recursion 14

  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/22/19 Always? When? More Recursion 15

  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""" Approach 1 Approach 2 5 341267 5341 267 commafy commafy 5 , 341,267 5,341 , 267 10/22/19 Always? When? More Recursion Always! 16

  17. 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/22/19 More Recursion 17

  18. 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/22/19 More Recursion 18

  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 = 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/22/19 More Recursion 19

  20. 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/22/19 More Recursion 20

  21. 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/22/19 More Recursion 21

  22. 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/22/19 More Recursion 22

  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 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/22/19 More Recursion 23

Recommend


More recommend