61a lecture 6
play

61A Lecture 6 Monday, February 2 Announcements 2 Announcements - PowerPoint PPT Presentation

61A Lecture 6 Monday, February 2 Announcements 2 Announcements Homework 2 due Monday 2/2 @ 11:59pm 2 Announcements Homework 2 due Monday 2/2 @ 11:59pm Project 1 due Thursday 2/5 @ 11:59pm 2 Announcements Homework 2 due Monday


  1. The Anatomy of a Recursive Function � • The def statement header is similar to other functions • Conditional statements check for base cases � • Base cases are evaluated without recursive calls � � def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 7

  2. The Anatomy of a Recursive Function � • The def statement header is similar to other functions • Conditional statements check for base cases � • Base cases are evaluated without recursive calls � • Recursive cases are evaluated with recursive calls � def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 7

  3. The Anatomy of a Recursive Function � • The def statement header is similar to other functions • Conditional statements check for base cases � • Base cases are evaluated without recursive calls � • Recursive cases are evaluated with recursive calls � def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 7

  4. The Anatomy of a Recursive Function � • The def statement header is similar to other functions • Conditional statements check for base cases � • Base cases are evaluated without recursive calls � • Recursive cases are evaluated with recursive calls � def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last (Demo) 7

  5. Recursion in Environment Diagrams

  6. Recursion in Environment Diagrams Interactive Diagram 9

  7. Recursion in Environment Diagrams (Demo) Interactive Diagram 9

  8. Recursion in Environment Diagrams (Demo) Interactive Diagram 9

  9. Recursion in Environment Diagrams (Demo) • The same function fact is called multiple times. Interactive Diagram 9

  10. Recursion in Environment Diagrams (Demo) • The same function fact is called multiple times. Interactive Diagram 9

  11. Recursion in Environment Diagrams (Demo) • The same function fact is called multiple times. • Different frames keep track of the different arguments in each call. Interactive Diagram 9

  12. Recursion in Environment Diagrams (Demo) • The same function fact is called multiple times. • Different frames keep track of the different arguments in each call. • What n evaluates to depends upon which is the current environment. Interactive Diagram 9

  13. Recursion in Environment Diagrams (Demo) • The same function fact is called multiple times. • Different frames keep track of the different arguments in each call. • What n evaluates to depends upon which is the current environment. Interactive Diagram 9

  14. Recursion in Environment Diagrams (Demo) • The same function fact is called multiple times. • Different frames keep track of the different arguments in each call. • What n evaluates to depends upon which is the current environment. • Each call to fact solves a simpler problem than the last: smaller n. Interactive Diagram 9

  15. Iteration vs Recursion 10

  16. Iteration vs Recursion Iteration is a special case of recursion 10

  17. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 10

  18. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: 10

  19. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: def fact_iter (n): � total, k = 1 , 1 � while k <= n: � total, k = total*k, k+ 1 � return total 10

  20. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � total, k = 1 , 1 � while k <= n: � total, k = total*k, k+ 1 � return total 10

  21. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) 10

  22. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) Math: 10

  23. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) n Y Math: n ! = k k =1 10

  24. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) n ( 1 if n = 0 Y Math: n ! = k n ! = n · ( n − 1)! otherwise k =1 10

  25. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) n ( 1 if n = 0 Y Math: n ! = k n ! = n · ( n − 1)! otherwise k =1 Names: 10

  26. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) n ( 1 if n = 0 Y Math: n ! = k n ! = n · ( n − 1)! otherwise k =1 Names: n, total, k, fact_iter 10

  27. Iteration vs Recursion Iteration is a special case of recursion 4! = 4 · 3 · 2 · 1 = 24 Using while: Using recursion: def fact_iter (n): � def fact (n): � total, k = 1 , 1 � if n == 0 : � while k <= n: � return 1 � total, k = total*k, k+ 1 � else : � return total return n * fact(n- 1 ) n ( 1 if n = 0 Y Math: n ! = k n ! = n · ( n − 1)! otherwise k =1 Names: n, total, k, fact_iter n, fact 10

  28. Verifying Recursive Functions

  29. The Recursive Leap of Faith 12

  30. The Recursive Leap of Faith 12 Photo by Kevin Lee, Preikestolen, Norway

  31. The Recursive Leap of Faith def fact (n): � if n == 0 : � return 1 � else : � return n * fact(n- 1 ) 12 Photo by Kevin Lee, Preikestolen, Norway

  32. The Recursive Leap of Faith def fact (n): � if n == 0 : � return 1 � else : � return n * fact(n- 1 ) Is fact implemented correctly? 12 Photo by Kevin Lee, Preikestolen, Norway

  33. The Recursive Leap of Faith def fact (n): � if n == 0 : � return 1 � else : � return n * fact(n- 1 ) Is fact implemented correctly? 1. Verify the base case. 12 Photo by Kevin Lee, Preikestolen, Norway

  34. The Recursive Leap of Faith def fact (n): � if n == 0 : � return 1 � else : � return n * fact(n- 1 ) Is fact implemented correctly? 1. Verify the base case. 2. Treat fact as a functional abstraction! 12 Photo by Kevin Lee, Preikestolen, Norway

  35. The Recursive Leap of Faith def fact (n): � if n == 0 : � return 1 � else : � return n * fact(n- 1 ) Is fact implemented correctly? 1. Verify the base case. 2. Treat fact as a functional abstraction! 3. Assume that fact(n-1) is correct. 12 Photo by Kevin Lee, Preikestolen, Norway

  36. The Recursive Leap of Faith def fact (n): � if n == 0 : � return 1 � else : � return n * fact(n- 1 ) Is fact implemented correctly? 1. Verify the base case. 2. Treat fact as a functional abstraction! 3. Assume that fact(n-1) is correct. 4. Verify that fact(n) is correct, assuming that fact(n-1) correct. 12 Photo by Kevin Lee, Preikestolen, Norway

  37. Verifying Digit Sum def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  38. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  39. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  40. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  41. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) for all k with fewer digits than n, (simpler case) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  42. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) for all k with fewer digits than n, (simpler case) sum_digits(n) will be sum_digits(n//10) plus the last digit of n. (conclusion) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  43. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) for all k with fewer digits than n, (simpler case) sum_digits(n) will be sum_digits(n//10) plus the last digit of n. (conclusion) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  44. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) for all k with fewer digits than n, (simpler case) sum_digits(n) will be sum_digits(n//10) plus the last digit of n. (conclusion) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  45. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) for all k with fewer digits than n, (simpler case) sum_digits(n) will be sum_digits(n//10) plus the last digit of n. (conclusion) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  46. Verifying Digit Sum The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. (base case) Assuming sum_digits(k) correctly sums the digits of k (abstraction) for all k with fewer digits than n, (simpler case) sum_digits(n) will be sum_digits(n//10) plus the last digit of n. (conclusion) def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 13

  47. Mutual Recursion

  48. The Luhn Algorithm 15

  49. The Luhn Algorithm Used to verify credit card numbers 15

  50. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm 15

  51. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). 15

  52. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). • Take the sum of all the digits. 15

  53. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). • Take the sum of all the digits. 1 3 8 7 4 3 15

  54. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). • Take the sum of all the digits. 1 3 8 7 4 3 2 3 1+6=7 7 8 3 15

  55. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). • Take the sum of all the digits. 1 3 8 7 4 3 = 30 2 3 1+6=7 7 8 3 15

  56. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). • Take the sum of all the digits. 1 3 8 7 4 3 = 30 2 3 1+6=7 7 8 3 The Luhn sum of a valid credit card number is a multiple of 10. 15

  57. The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5). • Take the sum of all the digits. 1 3 8 7 4 3 = 30 2 3 1+6=7 7 8 3 The Luhn sum of a valid credit card number is a multiple of 10. (Demo) 15

  58. Recursion and Iteration

  59. Converting Recursion to Iteration 17

  60. Converting Recursion to Iteration Can be tricky: Iteration is a special case of recursion. 17

  61. Converting Recursion to Iteration Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function. 17

  62. Converting Recursion to Iteration Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function. def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last 17

  63. Converting Recursion to Iteration Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function. def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last What's left to sum 17

  64. Converting Recursion to Iteration Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function. def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last A partial sum What's left to sum 17

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