61a lecture 4
play

61A Lecture 4 Friday, August 31 The Fibonacci Sequence 2 Example: - PowerPoint PPT Presentation

61A Lecture 4 Friday, August 31 The Fibonacci Sequence 2 Example: http://goo.gl/dcaf0 The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... 2 Example: http://goo.gl/dcaf0 The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n):


  1. 61A Lecture 4 Friday, August 31

  2. The Fibonacci Sequence 2 Example: http://goo.gl/dcaf0

  3. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... 2 Example: http://goo.gl/dcaf0

  4. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  5. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  6. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  7. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  8. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  9. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  10. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  11. The Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, ... def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr 2 Example: http://goo.gl/dcaf0

  12. Practical Guidance: the Art of the Function 3

  13. Practical Guidance: the Art of the Function 3

  14. Practical Guidance: the Art of the Function Give each function exactly one job. 3

  15. Practical Guidance: the Art of the Function Give each function exactly one job. vs 3

  16. Practical Guidance: the Art of the Function Give each function exactly one job. vs Don’t repeat yourself (DRY). Implement a computational process just once, but execute it many times. 3

  17. Practical Guidance: the Art of the Function Give each function exactly one job. vs Don’t repeat yourself (DRY). Implement a computational process just once, but execute it many times. 3

  18. Practical Guidance: the Art of the Function Give each function exactly one job. vs Don’t repeat yourself (DRY). Implement a computational process just once, but execute it many times. Define functions generally. 3

  19. Practical Guidance: the Art of the Function Give each function exactly one job. vs Don’t repeat yourself (DRY). Implement a computational process just once, but execute it many times. Define functions generally. 3

  20. Generalizing Patterns with Arguments 4

  21. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. 4

  22. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: 4

  23. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r 4

  24. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r 4

  25. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r 4

  26. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r Area: 4

  27. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r r 2 Area: 4

  28. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r π · r 2 r 2 Area: 4

  29. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r √ 3 3 π · r 2 · r 2 r 2 Area: 2 4

  30. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r √ 3 3 π · r 2 · r 2 1 · r 2 r 2 Area: 2 4

  31. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r √ 3 3 π · r 2 · r 2 1 · r 2 r 2 Area: 2 4

  32. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r √ 3 3 π · r 2 · r 2 1 · r 2 r 2 Area: 2 4

  33. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r √ 3 3 π · r 2 · r 2 1 · r 2 r 2 Area: 2 4

  34. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r √ 3 3 π · r 2 · r 2 1 · r 2 r 2 Area: 2 Finding common structure allows for shared implementation 4

  35. Generalizing Over Computational Processes 5

  36. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number. 5

  37. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number. 5 X k = 1 + 2 + 3 + 4 + 5 = 15 k =1 5 k 3 = 1 3 + 2 3 + 3 3 + 4 3 + 5 3 X = 225 k =1 5 (4 k − 3) · (4 k − 1) = 8 8 3 + 8 35 + 8 8 8 X 99 + 195 + = 3 . 04 323 k =1 5

  38. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number. 5 X k = 1 + 2 + 3 + 4 + 5 = 15 k =1 5 k 3 = 1 3 + 2 3 + 3 3 + 4 3 + 5 3 X = 225 k =1 5 (4 k − 3) · (4 k − 1) = 8 8 3 + 8 35 + 8 8 8 X 99 + 195 + = 3 . 04 323 k =1 5

  39. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number. 5 X k = 1 + 2 + 3 + 4 + 5 = 15 k =1 5 k 3 = 1 3 + 2 3 + 3 3 + 4 3 + 5 3 X = 225 k =1 5 (4 k − 3) · (4 k − 1) = 8 8 3 + 8 35 + 8 8 8 X 99 + 195 + = 3 . 04 323 k =1 5

  40. Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number. 5 X k = 1 + 2 + 3 + 4 + 5 = 15 k =1 5 k 3 = 1 3 + 2 3 + 3 3 + 4 3 + 5 3 X = 225 k =1 5 (4 k − 3) · (4 k − 1) = 8 8 3 + 8 35 + 8 8 8 X 99 + 195 + = 3 . 04 323 k =1 5

  41. Summation Example def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total − − 6 −−

  42. Summation Example Function of a single argument (not called term) def cube(k): return pow(k, 3) def summation(n, term): """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total − − 6 −−

  43. Summation Example Function of a single argument (not called term) def cube(k): return pow(k, 3) A formal parameter that def summation(n, term): will be bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total − − 6 −−

  44. Summation Example Function of a single argument (not called term) def cube(k): return pow(k, 3) A formal parameter that def summation(n, term): will be bound to a function """Sum the first n terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total The function bound to term gets called here − − 6 −−

Recommend


More recommend