cs 61a discussion 3
play

CS 61A Discussion 3 Recursion Albert Xu Attendance: - PowerPoint PPT Presentation

CS 61A Discussion 3 Recursion Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Recursion: an example Recursion: an example Here are the two key parts of recursion: 1) Base case 2) Recursive


  1. CS 61A Discussion 3 Recursion Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

  2. Announcements

  3. Recursion: an example

  4. Recursion: an example Here are the two key parts of recursion: 1) Base case 2) Recursive call Look for them in this example.

  5. Recursion: an example Base case

  6. Recursion: an example Base case Recursive call (and multiply it by n)

  7. Recursion: an example Base case Recursive call (and multiply it by n) This function works because n! = n * (n - 1)!, which is our recursive call.

  8. Recursion: dominoes 0 1 2 3 4 5 6 . . .

  9. Recursion: dominoes 0 1 2 3 4 5 6 . . .

  10. Recursion: dominoes Let’s calculate factorial(4)! 0 1 2 3 4 5 6 . . .

  11. Recursion: dominoes factorial(4) = 4 * factorial(3) 0 1 2 3 4 5 6 . . .

  12. Recursion: dominoes factorial(3) = 3 * factorial(2) 0 1 2 3 4 5 6 . . .

  13. Recursion: dominoes factorial(2) = 2 * factorial(1) 0 1 2 3 4 5 6 . . .

  14. Recursion: dominoes factorial(1) = 1 * factorial(0) 0 1 2 3 4 5 6 . . .

  15. Recursion: dominoes factorial(1) = 1 * 1 0 1 2 3 4 5 6 . . .

  16. Recursion: dominoes factorial(1) = 1 * 1 0 1 2 3 4 5 6 . . . When you’re writing code, assume the recursive call works!

  17. Approaching Recursion hard functions are hard to write.

  18. Approaching Recursion hard functions are hard to write. the first step is often the most difficult

  19. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Write triangular. This computes the n th triangular number, which is 0 + 1 + 2 + … + n

  20. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Steps: Write triangular. 1) Recursive call(s) - what is a This computes the n th convenient subproblem that our triangular number, which overall problem shrinks to? is 0 + 1 + 2 + … + n

  21. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Steps: Write triangular. 1) Recursive call(s) - what is a This computes the n th convenient subproblem that our triangular number, which overall problem shrinks to? is 0 + 1 + 2 + … + n

  22. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Steps: Write triangular. 1) Recursive call(s) - what is a This computes the n th convenient subproblem that our triangular number, which overall problem shrinks to? is 0 + 1 + 2 + … + n 2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem

  23. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Steps: Write triangular. 1) Recursive call(s) - what is a This computes the n th convenient subproblem that our triangular number, which overall problem shrinks to? is 0 + 1 + 2 + … + n 2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem

  24. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Steps: Write triangular. 1) Recursive call(s) - what is a This computes the n th convenient subproblem that our triangular number, which overall problem shrinks to? is 0 + 1 + 2 + … + n 2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem 3) Base case - when do I stop… i.e. what is the smallest case

  25. Approaching Recursion hard functions are hard to write. the first step is often the most difficult Steps: Write triangular. 1) Recursive call(s) - what is a This computes the n th convenient subproblem that our triangular number, which overall problem shrinks to? is 0 + 1 + 2 + … + n 2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem 3) Base case - when do I stop… i.e. what is the smallest case

  26. Thanks for coming. Have a great rest of your week! :) Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

Recommend


More recommend