CS61A Lecture 8 Amir Kamil UC Berkeley February 8, 2013 - - PowerPoint PPT Presentation

cs61a lecture 8
SMART_READER_LITE
LIVE PREVIEW

CS61A Lecture 8 Amir Kamil UC Berkeley February 8, 2013 - - PowerPoint PPT Presentation

CS61A Lecture 8 Amir Kamil UC Berkeley February 8, 2013 Announcements HW3 out, due Tuesday at 7pm Midterm next Wednesday at 7pm Keep an eye out for your assigned location Old exams posted Review sessions Saturday 2


slide-1
SLIDE 1

CS61A Lecture 8

Amir Kamil UC Berkeley February 8, 2013

slide-2
SLIDE 2

 HW3 out, due Tuesday at 7pm  Midterm next Wednesday at 7pm

 Keep an eye out for your assigned location  Old exams posted  Review sessions

 Saturday 2‐4pm in 2050 VLSB  Extended office hours Sunday 11‐3pm in 310 Soda  HKN review session Sunday 3‐6pm in 145 Dwinelle

 Environment diagram handout on website  Code review system online

 See Piazza post for details

Announcements

slide-3
SLIDE 3

Newton’s Method

Begin with a function f and an initial guess x

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

slide-4
SLIDE 4

Newton’s Method

Begin with a function f and an initial guess x

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

slide-5
SLIDE 5

Newton’s Method

Begin with a function f and an initial guess x

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x)

slide-6
SLIDE 6

Newton’s Method

Begin with a function f and an initial guess x

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x) Compute the derivative of f at the guess: f'(x)

slide-7
SLIDE 7

Newton’s Method

Begin with a function f and an initial guess x

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x) Compute the derivative of f at the guess: f'(x) Update guess to be:

slide-8
SLIDE 8

Newton’s Method

Begin with a function f and an initial guess x (x, f(x))

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x) Compute the derivative of f at the guess: f'(x) Update guess to be:

slide-9
SLIDE 9

Newton’s Method

Begin with a function f and an initial guess x (x, f(x)) ‐f(x)

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x) Compute the derivative of f at the guess: f'(x) Update guess to be:

slide-10
SLIDE 10

Newton’s Method

Begin with a function f and an initial guess x (x, f(x)) ‐f(x)/f'(x) ‐f(x)

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x) Compute the derivative of f at the guess: f'(x) Update guess to be:

slide-11
SLIDE 11

Newton’s Method

Begin with a function f and an initial guess x (x, f(x)) ‐f(x)/f'(x) ‐f(x)

Visualization: http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Compute the value of f at the guess: f(x) Compute the derivative of f at the guess: f'(x) Update guess to be:

slide-12
SLIDE 12

Special Case: Square Roots

slide-13
SLIDE 13

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a

slide-14
SLIDE 14

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update:

slide-15
SLIDE 15

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update:

slide-16
SLIDE 16

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update: x ‐ f(x)/f'(x)

slide-17
SLIDE 17

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update: Babylonian Method x ‐ f(x)/f'(x)

slide-18
SLIDE 18

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Implementation questions: Update: Babylonian Method x ‐ f(x)/f'(x)

slide-19
SLIDE 19

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a What guess should start the computation? Implementation questions: Update: Babylonian Method x ‐ f(x)/f'(x)

slide-20
SLIDE 20

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a What guess should start the computation? How do we know when we are finished? Implementation questions: Update: Babylonian Method x ‐ f(x)/f'(x)

slide-21
SLIDE 21

Special Case: Cube Roots

slide-22
SLIDE 22

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a

slide-23
SLIDE 23

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Update:

slide-24
SLIDE 24

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Update:

slide-25
SLIDE 25

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Update: x ‐ f(x)/f'(x)

slide-26
SLIDE 26

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Implementation questions: Update: x ‐ f(x)/f'(x)

slide-27
SLIDE 27

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a What guess should start the computation? Implementation questions: Update: x ‐ f(x)/f'(x)

slide-28
SLIDE 28

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a What guess should start the computation? How do we know when we are finished? Implementation questions: Update: x ‐ f(x)/f'(x)

slide-29
SLIDE 29

Iterative Improvement

 

slide-30
SLIDE 30

Iterative Improvement

First, identify common structure.

slide-31
SLIDE 31

Iterative Improvement

First, identify common structure. Then define a function that generalizes the procedure.

slide-32
SLIDE 32

Iterative Improvement

def iter_improve(update, done, guess=1, max_updates=1000): """Iteratively improve guess with update until done returns a true value. >>> iter_improve(golden_update, golden_test) 1.618033988749895 """ k = 0 while not done(guess) and k < max_updates: guess = update(guess) k = k + 1 return guess

First, identify common structure. Then define a function that generalizes the procedure.

slide-33
SLIDE 33

Newton’s Method for nth Roots

slide-34
SLIDE 34

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a): def root_func(x): return pow(x, n) - a def derivative(x): return n * pow(x, n-1) return root_func, derivative def nth_root_newton(a, n): """Return the nth root of a. >>> nth_root_newton(8, 3) 2.0 """ root_func, deriv = nth_root_func_and_derivative(n, a) def update(x): return x - root_func(x) / deriv(x) def done(x): return root_func(x) == 0 return iter_improve(update, done)

slide-35
SLIDE 35

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a): def root_func(x): return pow(x, n) - a def derivative(x): return n * pow(x, n-1) return root_func, derivative def nth_root_newton(a, n): """Return the nth root of a. >>> nth_root_newton(8, 3) 2.0 """ root_func, deriv = nth_root_func_and_derivative(n, a) def update(x): return x - root_func(x) / deriv(x) def done(x): return root_func(x) == 0 return iter_improve(update, done)

Exact derivative

slide-36
SLIDE 36

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a): def root_func(x): return pow(x, n) - a def derivative(x): return n * pow(x, n-1) return root_func, derivative def nth_root_newton(a, n): """Return the nth root of a. >>> nth_root_newton(8, 3) 2.0 """ root_func, deriv = nth_root_func_and_derivative(n, a) def update(x): return x - root_func(x) / deriv(x) def done(x): return root_func(x) == 0 return iter_improve(update, done)

x – f(x)/f’(x) Exact derivative

slide-37
SLIDE 37

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a): def root_func(x): return pow(x, n) - a def derivative(x): return n * pow(x, n-1) return root_func, derivative def nth_root_newton(a, n): """Return the nth root of a. >>> nth_root_newton(8, 3) 2.0 """ root_func, deriv = nth_root_func_and_derivative(n, a) def update(x): return x - root_func(x) / deriv(x) def done(x): return root_func(x) == 0 return iter_improve(update, done)

x – f(x)/f’(x) Definition of a function zero Exact derivative

slide-38
SLIDE 38

Factorial

slide-39
SLIDE 39

The factorial of a non‐negative integer n is

Factorial

slide-40
SLIDE 40

The factorial of a non‐negative integer n is

Factorial

slide-41
SLIDE 41

The factorial of a non‐negative integer n is

Factorial

slide-42
SLIDE 42

The factorial of a non‐negative integer n is

  

Factorial

slide-43
SLIDE 43

The factorial of a non‐negative integer n is

  

Factorial

slide-44
SLIDE 44

The factorial of a non‐negative integer n is This is called a recurrence relation;

 

Factorial

slide-45
SLIDE 45

The factorial of a non‐negative integer n is This is called a recurrence relation; Factorial is defined in terms of itself

Factorial

slide-46
SLIDE 46

The factorial of a non‐negative integer n is This is called a recurrence relation; Factorial is defined in terms of itself Can we write code to compute factorial using the same pattern?

Factorial

slide-47
SLIDE 47

Computing Factorial

slide-48
SLIDE 48

We can compute factorial using the direct definition

Computing Factorial

slide-49
SLIDE 49

We can compute factorial using the direct definition

Computing Factorial

slide-50
SLIDE 50

We can compute factorial using the direct definition

Computing Factorial

def factorial(n): if n == 0 or n == 1: return 1 total = 1 while n >= 1: total, n = total * n, n - 1 return total

slide-51
SLIDE 51

 

Computing Factorial

slide-52
SLIDE 52

Can we compute it using the recurrence relation?

Computing Factorial

slide-53
SLIDE 53

Can we compute it using the recurrence relation?

Computing Factorial

slide-54
SLIDE 54

Can we compute it using the recurrence relation?

Computing Factorial

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-55
SLIDE 55

Can we compute it using the recurrence relation?

Computing Factorial

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-56
SLIDE 56

Can we compute it using the recurrence relation? This is much shorter! But can a function call itself?

Computing Factorial

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-57
SLIDE 57

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

slide-58
SLIDE 58

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

slide-59
SLIDE 59

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

slide-60
SLIDE 60

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

slide-61
SLIDE 61

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4!

slide-62
SLIDE 62

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3!

slide-63
SLIDE 63

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3! Compute 2!

slide-64
SLIDE 64

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3! Compute 2! Compute 1!

slide-65
SLIDE 65

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3! Compute 2! Compute 1!

slide-66
SLIDE 66

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3! Compute 2! Compute 1!

slide-67
SLIDE 67

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3! Compute 2! Compute 1!

slide-68
SLIDE 68

Let’s see what happens!

Factorial Environment Diagram

Example: http://goo.gl/NjCKG

Compute 4! Compute 3! Compute 2! Compute 1!

slide-69
SLIDE 69

 

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-70
SLIDE 70

A function is recursive if the body calls the function itself, either directly or indirectly

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-71
SLIDE 71

A function is recursive if the body calls the function itself, either directly or indirectly Recursive functions have two important components:

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-72
SLIDE 72

A function is recursive if the body calls the function itself, either directly or indirectly Recursive functions have two important components:

1.

Base case(s), where the function directly computes an answer without calling itself

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1)

slide-73
SLIDE 73

A function is recursive if the body calls the function itself, either directly or indirectly Recursive functions have two important components:

1.

Base case(s), where the function directly computes an answer without calling itself

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) Base case

slide-74
SLIDE 74

A function is recursive if the body calls the function itself, either directly or indirectly Recursive functions have two important components:

1.

Base case(s), where the function directly computes an answer without calling itself

2.

Recursive case(s), where the function calls itself as part of the computation

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) Base case

slide-75
SLIDE 75

A function is recursive if the body calls the function itself, either directly or indirectly Recursive functions have two important components:

1.

Base case(s), where the function directly computes an answer without calling itself

2.

Recursive case(s), where the function calls itself as part of the computation

Recursive Functions

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) Recursive case Base case

slide-76
SLIDE 76

   

Practical Guidance: Choosing Names

slide-77
SLIDE 77

Names typically don’t matter for correctness, but they matter tremendously for legibility

 

Practical Guidance: Choosing Names

slide-78
SLIDE 78

Names typically don’t matter for correctness, but they matter tremendously for legibility

 

Practical Guidance: Choosing Names

boolean d play_helper

slide-79
SLIDE 79

Names typically don’t matter for correctness, but they matter tremendously for legibility

 

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

slide-80
SLIDE 80

Names typically don’t matter for correctness, but they matter tremendously for legibility Use names for repeated compound expressions

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

slide-81
SLIDE 81

Names typically don’t matter for correctness, but they matter tremendously for legibility Use names for repeated compound expressions

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

if sqrt(square(a) + square(b)) > 1: x = x + sqrt(square(a) + square(b))

slide-82
SLIDE 82

Names typically don’t matter for correctness, but they matter tremendously for legibility Use names for repeated compound expressions

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

if sqrt(square(a) + square(b)) > 1: x = x + sqrt(square(a) + square(b)) h = sqrt(square(a) + square(b)) if h > 1: x = x + h

slide-83
SLIDE 83

Names typically don’t matter for correctness, but they matter tremendously for legibility Use names for repeated compound expressions Use names for meaningful parts of compound expressions

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

if sqrt(square(a) + square(b)) > 1: x = x + sqrt(square(a) + square(b)) h = sqrt(square(a) + square(b)) if h > 1: x = x + h

slide-84
SLIDE 84

Names typically don’t matter for correctness, but they matter tremendously for legibility Use names for repeated compound expressions Use names for meaningful parts of compound expressions

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

if sqrt(square(a) + square(b)) > 1: x = x + sqrt(square(a) + square(b)) h = sqrt(square(a) + square(b)) if h > 1: x = x + h x = (-b + sqrt(square(b) - 4 * a * c)) / (2 * a)

slide-85
SLIDE 85

Names typically don’t matter for correctness, but they matter tremendously for legibility Use names for repeated compound expressions Use names for meaningful parts of compound expressions

Practical Guidance: Choosing Names

boolean turn_is_over d dice play_helper take_turn

if sqrt(square(a) + square(b)) > 1: x = x + sqrt(square(a) + square(b)) h = sqrt(square(a) + square(b)) if h > 1: x = x + h x = (-b + sqrt(square(b) - 4 * a * c)) / (2 * a) disc_term = sqrt(square(b) - 4 * a * c) x = (-b + disc_term) / (2 * a)

slide-86
SLIDE 86

Practical Guidance: DRY

slide-87
SLIDE 87

Sometimes, removing repetition requires restructuring the code

Practical Guidance: DRY

slide-88
SLIDE 88

Sometimes, removing repetition requires restructuring the code

Practical Guidance: DRY

def find_quadratic_root(a, b, c, plus=True): """Applies the quadratic formula to the polynomial ax^2 + bx + c.""" if plus: return (-b + sqrt(square(b) - 4 * a * c)) / (2 * a) else: return (-b - sqrt(square(b) - 4 * a * c)) / (2 * a)

slide-89
SLIDE 89

Sometimes, removing repetition requires restructuring the code

Practical Guidance: DRY

def find_quadratic_root(a, b, c, plus=True): """Applies the quadratic formula to the polynomial ax^2 + bx + c.""" if plus: return (-b + sqrt(square(b) - 4 * a * c)) / (2 * a) else: return (-b - sqrt(square(b) - 4 * a * c)) / (2 * a)

slide-90
SLIDE 90

Sometimes, removing repetition requires restructuring the code

Practical Guidance: DRY

def find_quadratic_root(a, b, c, plus=True): """Applies the quadratic formula to the polynomial ax^2 + bx + c.""" if plus: return (-b + sqrt(square(b) - 4 * a * c)) / (2 * a) else: return (-b - sqrt(square(b) - 4 * a * c)) / (2 * a) def find_quadratic_root(a, b, c, plus=True): """Applies the quadratic formula to the polynomial ax^2 + bx + c.""" disc_term = sqrt(square(b) - 4 * a * c) if not plus: disc_term *= -1 return (-b + disc_term) / (2 * a)

slide-91
SLIDE 91

 

 

Test‐Driven Development

slide-92
SLIDE 92

Write the test of a function before you write a function

 

 

Test‐Driven Development

slide-93
SLIDE 93

Write the test of a function before you write a function

A test will clarify the (one) job of the function

 

Test‐Driven Development

slide-94
SLIDE 94

Write the test of a function before you write a function

A test will clarify the (one) job of the function Your tests can help identify tricky edge cases

 

Test‐Driven Development

slide-95
SLIDE 95

Write the test of a function before you write a function

A test will clarify the (one) job of the function Your tests can help identify tricky edge cases

Develop incrementally and test each piece before moving on

 

Test‐Driven Development

slide-96
SLIDE 96

Write the test of a function before you write a function

A test will clarify the (one) job of the function Your tests can help identify tricky edge cases

Develop incrementally and test each piece before moving on

You can’t depend upon code that hasn’t been tested

Test‐Driven Development

slide-97
SLIDE 97

Write the test of a function before you write a function

A test will clarify the (one) job of the function Your tests can help identify tricky edge cases

Develop incrementally and test each piece before moving on

You can’t depend upon code that hasn’t been tested Run your old tests again after you make new changes

Test‐Driven Development