lecture 16 more recursion
play

Lecture 16: More Recursion! CS 1110 Introduction to Computing - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 16: More Recursion! CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Recursion Recursive Function : A


  1. http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 16: More Recursion! CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

  2. Recursion Recursive Function : A function that calls itself (directly or indirectly) Recursive Definition : A definition that is defined in terms of itself 2

  3. A Mathematical Example: Factorial Non-recursive definition: n! = n � n-1 � … � 2 � 1 = n (n-1 � … � 2 � 1) Recursive definition: n! = n (n-1)! for n > 0 Recursive case 0! = 1 Base case What happens if there is no base case? 3

  4. Recursive Call Frames factorial def factorial(n): 1 """Returns: factorial of n. n 3 Precondition: n ≥ 0 an int""" if n == 0: 1 return 1 2 return n*factorial(n-1) 3 factorial(3) 4

  5. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Precondition: n ≥ 0 an int""" if n == 0: 1 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 5

  6. Recursion factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Precondition: n ≥ 0 an int""" if n == 0: 1 return 1 2 Now what? return n*factorial(n-1) 3 Each call is a new frame. factorial(3) 6

  7. What happens next? (Q) A: B: factorial factorial 1, 3 1, 3, 1 û n n 3 3 2 factorial 1 D: n 2 factorial 1, 3, 1 û n 3 2 C: ERASE FRAME factorial 1, 3 factorial factorial 1 1 n 3 n n 3 2 7

  8. What happens next? (A) A: B: CORRECT factorial factorial 1, 3 1, 3, 1 û n n 3 3 2 factorial 1 D: n 2 factorial 1, 3, 1 û n 3 2 C: ERASE FRAME factorial 1, 3 factorial factorial 1 1 n 3 n n 3 2 8

  9. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1 if n == 0: 1 n 2 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 9

  10. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 10

  11. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1 return n*factorial(n-1) n 1 3 Call: factorial(3) 11

  12. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 Call: factorial(3) 12

  13. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1 Call: factorial(3) n 0 13

  14. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1, 2 Call: factorial(3) n 0 14

  15. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 15

  16. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 16

  17. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 RETURN 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 17

  18. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 RETURN 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 18

  19. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 RETURN 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 19

  20. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 RETURN 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 20

  21. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 6 RETURN Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 RETURN 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 21

  22. Recursive Call Frames factorial def factorial(n): 1, 3 """Returns: factorial of n. n 3 6 RETURN Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 RETURN 1 3 factorial 1, 2 Call: factorial(3) n 0 RETURN 1 22

  23. 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! 23

  24. Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case ! o l l e H # 2. Break into two parts # 3. Combine the result 24

  25. Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case left H e l l o ! # 2. Break into two parts right ! o l l e left = reverse(s[0]) right = reverse(s[1:]) If this is how we break it up…. # 3. Combine the result How do we combine it? 25

  26. How to Combine? (Q) def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case left H e l l o ! # 2. Break into two parts right ! o l l e left = reverse(s[0]) right = reverse(s[1:]) # 3. Combine the result return A: left + right B: right + left C: left D: right 26

  27. How to Combine? (A) def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case left H e l l o ! # 2. Break into two parts right ! o l l e left = reverse(s[0]) right = reverse(s[1:]) CORRECT # 3. Combine the result return A: left + right B: right + left C: left D: right 27

  28. Example: Reversing a String def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case left H e l l o ! # 2. Break into two parts right ! o l l e left = reverse(s[0]) right = reverse(s[1:]) # 3. Combine the result return right+left 28

  29. What is the Base Case? (Q) def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case 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 29

  30. What is the Base Case? (A) def reverse(s): H e l l o ! """Returns: reverse of s Precondition: s a string""" # 1. Handle base case CORRECT 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 30

  31. Example: Reversing a String def reverse(s): """Returns: reverse of s Precondition: s a string""" # 1. Handle base case if len(s) <= 1: Base Case return s # 2. Break into two parts left = reverse(s[0]) s[0] right = reverse(s[1:]) Recursive Case # 3. Combine the result return right+left 31

  32. Alternate Implementation (Q) def reverse(s): """Returns: reverse of s Does this work? Precondition: s a string""" # 1. Handle base case A: YES 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 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend