61a lecture 4
play

61A Lecture 4 Monday, September 9 Announcements Homework 1 due - PowerPoint PPT Presentation

61A Lecture 4 Monday, September 9 Announcements Homework 1 due Tuesday 9/10 at 5pm; Late homework is not accepted! Quiz on Wednesday 9/11 released at 1pm, due Thursday 9/12 at 11:59pm Open-computer : You can use the Python


  1. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! 5 Example: http://goo.gl/38ch3o

  2. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) k · ( k − 1) · ( k − 2) · . . . · 2 · 1 5 Example: http://goo.gl/38ch3o

  3. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 5 Example: http://goo.gl/38ch3o

  4. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ 5 Example: http://goo.gl/38ch3o

  5. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 5 Example: http://goo.gl/38ch3o

  6. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 5 Example: http://goo.gl/38ch3o

  7. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: 5 Example: http://goo.gl/38ch3o

  8. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 5 Example: http://goo.gl/38ch3o

  9. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 ways, total = ways * ______________________ , total - 1 return ways 5 Example: http://goo.gl/38ch3o

  10. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 total // selected ways, total = ways * ______________________ , total - 1 return ways 5 Example: http://goo.gl/38ch3o

  11. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 total // selected ways, total = ways * ______________________ , total - 1 return ways 5 Example: http://goo.gl/38ch3o

  12. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 total // selected ways, total = ways * ______________________ , total - 1 return ways 5 Example: http://goo.gl/38ch3o

  13. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 while selected < selection: selected = selected + 1 total // selected ways, total = ways * ______________________ , total - 1 return ways 5 Example: http://goo.gl/38ch3o

  14. Discussion Question Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) 38760 """ ways = 1 selected = 0 ... while selected < selection: ... selected = selected + 1 total // selected ways, total = ways * ______________________ , total - 1 return ways 5 Example: http://goo.gl/38ch3o

  15. Default Arguments (Demo)

  16. Designing Functions

  17. Characteristics of Functions 8

  18. Characteristics of Functions A function's domain is the set of all inputs it might possibly take as arguments. 8

  19. Characteristics of Functions A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. 8

  20. Characteristics of Functions A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. 8

  21. Characteristics of Functions def square(x): """Return X * X.""" A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. 8

  22. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. 8

  23. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. x is a number A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. 8

  24. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. n and d are positive integers with x is a number n greater than or equal to d. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. 8

  25. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. n and d are positive integers with x is a number n greater than or equal to d. A function's range is the set of output values it might possibly return. return value is a positive number A pure function's behavior is the relationship it creates between input and output. 8

  26. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. n and d are positive integers with x is a number n greater than or equal to d. A function's range is the set of output values it might possibly return. return value is a return value is a positive integer positive number A pure function's behavior is the relationship it creates between input and output. 8

  27. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. n and d are positive integers with x is a number n greater than or equal to d. A function's range is the set of output values it might possibly return. return value is a return value is a positive integer positive number A pure function's behavior is the relationship it creates between input and output. return value is the square of the input 8

  28. Characteristics of Functions def square(x): def choose(n, d): """Return X * X.""" """Return the number of ways to choose D of N items.""" A function's domain is the set of all inputs it might possibly take as arguments. n and d are positive integers with x is a number n greater than or equal to d. A function's range is the set of output values it might possibly return. return value is a return value is a positive integer positive number A pure function's behavior is the relationship it creates between input and output. return value is the return value is the number of ways square of the input to choose d of n items. 8

  29. A Guide to Designing Function 9

  30. A Guide to Designing Function Give each function exactly one job. 9

  31. A Guide to Designing Function Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. 9

  32. A Guide to Designing Function Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. 9

  33. A Guide to Designing Function Give each function exactly one job. Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. 9

  34. A Guide to Designing Function Give each function exactly one job. not Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. 9

  35. A Guide to Designing Function Give each function exactly one job. not Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. 9

  36. A Guide to Designing Function Give each function exactly one job. not Don’t repeat yourself (DRY). Implement a process just once, but execute it many times. Define functions generally. 9

  37. Generalization

  38. Generalizing Patterns with Arguments 11

  39. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. 11

  40. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: 11

  41. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r 11

  42. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r 11

  43. Generalizing Patterns with Arguments Regular geometric shapes relate length and area. Shape: r r r 11

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

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

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

  47. 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 11

  48. 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 11

  49. 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 11

  50. 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 11

  51. 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 11

  52. 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 11

  53. 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 (Demo) 11

  54. Higher-Order Functions

  55. Generalizing Over Computational Processes 13

  56. Generalizing Over Computational Processes The common structure among functions may be a computational process, rather than a number. 13

  57. Generalizing Over Computational Processes The common structure among functions may 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 13

  58. Generalizing Over Computational Processes The common structure among functions may 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 13

  59. Generalizing Over Computational Processes The common structure among functions may 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 13

  60. Generalizing Over Computational Processes The common structure among functions may 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 13

  61. Generalizing Over Computational Processes The common structure among functions may 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 (Demo) 13

  62. 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 − − 14 −−

  63. Summation Example Function of a single argument def cube(k): (not called term) 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 − − 14 −−

  64. Summation Example Function of a single argument def cube(k): (not called term) return pow(k, 3) A formal parameter that will def summation(n, term): 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 − − 14 −−

  65. Summation Example Function of a single argument def cube(k): (not called term) return pow(k, 3) A formal parameter that will def summation(n, term): 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 − − 14 −−

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

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

  68. Functions as Return Values (Demo)

  69. Locally Defined Functions 16

  70. Locally Defined Functions Functions defined within other function bodies are bound to names in a local frame 16

  71. Locally Defined Functions Functions defined within other function bodies are bound to names in a local frame − − def make_adder(n): """Return a function that takes one argument k and returns k + n. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder −− 16

Recommend


More recommend