61a lecture 6 announcements recursive functions recursive
play

61A Lecture 6 Announcements Recursive Functions Recursive - PowerPoint PPT Presentation

61A Lecture 6 Announcements Recursive Functions Recursive Functions 4 Recursive Functions Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly 4 Recursive Functions Definition:


  1. 61A Lecture 6

  2. Announcements

  3. Recursive Functions

  4. Recursive Functions 4

  5. Recursive Functions Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly 4

  6. Recursive Functions Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly Implication: Executing the body of a recursive function may require applying that function 4

  7. Recursive Functions Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly Implication: Executing the body of a recursive function may require applying that function 4

  8. Recursive Functions Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly Implication: Executing the body of a recursive function may require applying that function Drawing Hands, by M. C. Escher (lithograph, 1948) 4

  9. Digit Sums 2+0+1+5 = 8 5

  10. Digit Sums 2+0+1+5 = 8 • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9 5

  11. Digit Sums 2+0+1+5 = 8 • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9 • Useful for typo detection! 5

  12. Digit Sums 2+0+1+5 = 8 • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9 • Useful for typo detection! The Bank of 61A 1234 5678 9098 7658 OSKI THE BEAR 5

  13. Digit Sums 2+0+1+5 = 8 • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9 • Useful for typo detection! The Bank of 61A A checksum digit is a function of all the other 1234 5678 9098 7658 digits; It can be computed to detect typos OSKI THE BEAR 5

  14. Digit Sums 2+0+1+5 = 8 • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9 • Useful for typo detection! The Bank of 61A A checksum digit is a function of all the other 1234 5678 9098 7658 digits; It can be computed to detect typos OSKI THE BEAR • Credit cards actually use the Luhn algorithm, which we'll implement after digit_sum 5

  15. Sum Digits Without a While Statement 6

  16. Sum Digits Without a While Statement def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 6

  17. Sum Digits Without a While Statement def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 def sum_digits(n): """Return the sum of the digits of positive integer n.""" 6

  18. Sum Digits Without a While Statement def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n 6

  19. Sum Digits Without a While Statement def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 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) 6

  20. Sum Digits Without a While Statement def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 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 6

  21. The Anatomy of a Recursive 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 7

  22. The Anatomy of a Recursive Function • The def statement header is similar to other functions 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

  23. The Anatomy of a Recursive Function • The def statement header is similar to other functions 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

  24. The Anatomy of a Recursive Function • The def statement header is similar to other functions • Conditional statements check for base cases 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

  25. The Anatomy of a Recursive Function • The def statement header is similar to other functions • Conditional statements check for base cases 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. Recursion in Environment Diagrams

  32. Recursion in Environment Diagrams Interactive Diagram 9

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

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

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

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

  37. 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

  38. 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 
 the current environment Interactive Diagram 9

  39. 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 
 the current environment Interactive Diagram 9

  40. 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 
 the current environment • Each call to fact solves a simpler problem than the last: smaller n Interactive Diagram 9

  41. Iteration vs Recursion 10

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

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

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

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