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): """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
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
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
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
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
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
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
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
Practical Guidance: the Art of the Function 3
Practical Guidance: the Art of the Function 3
Practical Guidance: the Art of the Function Give each function exactly one job. 3
Practical Guidance: the Art of the Function Give each function exactly one job. vs 3
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
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
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
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
Generalizing Patterns with Arguments 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r Area: 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r r 2 Area: 4
Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r π · r 2 r 2 Area: 4
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
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
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
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
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
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
Generalizing Over Computational Processes 5
Generalizing Over Computational Processes The common structure among functions may itself be a computational process, rather than a number. 5
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
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
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
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
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 −−
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 −−
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 −−
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