section 3
play

section 3 attendance (no password today) - PowerPoint PPT Presentation

CS61A section 3 attendance (no password today) http://links.cs61a.org/jasonxu upcoming hw 3 hog contest ~ optional CS61A my thoughts definitely di ffi cult midterm recovery points MT1 12% CS61A my thoughts 10% 20% +3% 25%


  1. CS61A section 3 attendance (no password today) http://links.cs61a.org/jasonxu upcoming hw 3 hog contest ~ optional

  2. CS61A my thoughts 🤭 definitely di ffi cult midterm recovery points

  3. MT1 12% CS61A my thoughts 10% 20% +3% 25% 🤭 definitely di ffi cult 61A has a lot of resources Projects 33%

  4. CS61A recursion things defined by themselves

  5. def factorial(n): if n == 0: return 1 else: CS61A recursion return n * factorial(n - 1) 5! = 5 * 4! factorial! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0!

  6. def factorial(n): if n == 0: return 1 else: CS61A recursion return n * factorial(n - 1) uhhhhhhhhh 5! = 5 * 4! factorial! when do i stop? 4! = 4 * 3! 3! = 3 * 2! 0! = 0 * -1! 2! = 2 * 1! -1! = -1 * -2! … 1! = 1 * 0!

  7. def factorial(n): if n == 0: return 1 else: CS61A recursion return n * factorial(n - 1) uhhhhhhhhh 5! = 5 * 4! factorial! base case! 4! = 4 * 3! 3! = 3 * 2! 0! = 0 * -1! 2! = 2 * 1! -1! = -1 * -2! … 1! = 1 * 0!

  8. def factorial(n): if n == 0: return 1 else: CS61A recursion return n * factorial(n - 1) yay! 😎 5! = 5 * 4! factorial! base case! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 0! = 1 1! = 1 * 0!

  9. CS61A recursion how do we come up with this factorial! def factorial(n): by definition, 😲 if n == 0: return 1 by assuming it works, 🤫 else: return n * factorial(n - 1) pattern: how can we solve sub-problems to solve the current problem?

  10. how do we calculate 5! 5! = 5 * 4! CS61A recursion for this to be true, don’t we have to assume that ‘!’ really does what it says factorial! well in code we can’t name a function ‘!’ def factorial(n): we assume that (n-1)! works if n == 0: return 1 recursive leap of faith else: return n * factorial(n - 1) well… i have to test it by tracing it well… big headache

  11. CS61A recursion if you capture all the base cases you can assume it works strategy so you can create the recursive call

  12. CS61A recursion analogy black friday shopping… long line you want to know how many people in front accurately, you only know if you’re the first person otherwise, you have to ask the person in front of you for their position is this a good recursive procedure…?

  13. CS61A recursion analogy black friday shopping… long line you want to know how many people in front accurately, you only know if you’re the first person otherwise, you have to ask the person in front of you for their position and add 1 is this a recursive procedure…?

  14. CS61A recursion motivation for it operation input

  15. CS61A recursion things defined by themselves output operation input

  16. CS61A recursion things defined by themselves output :) operation input

  17. CS61A recursion things defined by themselves output :) operation input tell me the number of ways to line $26

  18. ? CS61A recursion things defined by themselves count :) operation input = 26 tell me the number of ways to line $26

  19. i have to figure out how to get $26 the blue boxes are operations! if 0: +1 if i use $1 as my first denomination if 0: +1 i have to figure out how to get the $25 . . . if i use $1 as my first denomination if i use $20 as my first denomination i have to figure out how to get the $24 i have to figure out how to get the $6 . . .

  20. i have to figure out how to get $26 if 0: +1 if i use $1 as my first denomination if 0: +1 i have to figure out how to get the $25 . . . if i use $1 as my first denomination if i use $20 as my first denomination i have to figure out how to get the $24 i have to figure out how to get the $6 . . . output

  21. i have to figure out how to get $26 if 0: +1 if i use $1 as my first denomination if 0: +1 i have to figure out how to get the $25 . . . if i use $1 as my first denomination if i use $20 as my first denomination i have to figure out how to get the $24 i have to figure out how to get the $6 . . . output :(

  22. def count(n): total = 0 options = [n] while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0: options.append(val) return total

  23. def count(n): total = 0 🙄 options = [n] while len(options) > 0: curr = options.pop(0) for change in [1, 5, 10, 20]: val = curr - change if val == 0: total += 1 elif val > 0: options.append(val) return total

  24. ? CS61A recursion things defined by themselves count :) operation input = 26

  25. ? CS61A recursion things defined by themselves count :) function input = 26

  26. i have to figure out how to get $26 the blue box is a function! if 0: +1 CS61A recursion things defined by themselves if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6

  27. i have to figure out how to get $26 recursive leap of faith if 0: +1 CS61A recursion things defined by themselves if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6

  28. i have to figure out how to get $26 recursive leap of faith math (out of scope) if 0: +1 CS61A any recursive problem recursion you get is recursively possible things defined by themselves if i use $1 as my first denomination i have to figure out how to get the $25 . . . if i use $20 as my first denomination i have to figure out how to get the $6

  29. i have to figure out how to get $26 if 0: +1 count CS61A if < 0: +0 recursion things defined by themselves def count_recurse(n): if i use $1 as my first denomination if n < 0: i have to figure out how to get the $25 return 0 elif n == 0: . return 1 . else: . return count_recurse(n - 1) + count_recurse(n - 5) if i use $20 as my first denomination + count_recurse(n - 10) i have to figure out how to get the $6 + count_recurse(n - 20)

  30. def count(n): def count_recurse(n): total = 0 if n < 0: options = [n] return 0 while len(options) > 0: elif n == 0: curr = options.pop(0) return 1 for change in [1, 5, 10, 20]: else: val = curr - change return count_recurse(n - 1) if val == 0: + count_recurse(n - 5) total += 1 + count_recurse(n - 10) elif val > 0: + count_recurse(n - 20) options.append(val) return total

  31. def count(n): def count_recurse(n): total = 0 if n < 0: options = [n] return 0 while len(options) > 0: elif n == 0: curr = options.pop(0) return 1 for change in [1, 5, 10, 20]: else: val = curr - change return count_recurse(n - 1) if val == 0: + count_recurse(n - 5) total += 1 + count_recurse(n - 10) elif val > 0: + count_recurse(n - 20) options.append(val) return total function function

  32. ♥ def count(n): def count_recurse(n): total = 0 if n < 0: options = [n] return 0 while len(options) > 0: elif n == 0: curr = options.pop(0) return 1 for change in [1, 5, 10, 20]: else: val = curr - change return count_recurse(n - 1) if val == 0: + count_recurse(n - 5) total += 1 + count_recurse(n - 10) elif val > 0: + count_recurse(n - 20) options.append(val) return total

  33. so what does this mean we have a strategy on how to create recursive functions we can see that recursion isn’t pointless… at least for more complex problems

  34. $26 CS61A recursion subproblem $6 $25 things defined by themselves $16 $21 def count_recurse(n): if n < 0: return 0 elif n == 0: return 1 else: return count_recurse(n - 1) + count_recurse(n - 5) + count_recurse(n - 10) + count_recurse(n - 20)

  35. CS61A recursion things defined by themselves 1.set up rules (base cases) 2.assume it works

  36. CS61A recursion what does this mean 🤰

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