61A Lecture 6 Monday, February 2 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 6
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 6 Monday, February 2 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 6 Monday, February 2 Announcements 2 Announcements Homework 2 due Monday 2/2 @ 11:59pm 2 Announcements Homework 2 due Monday 2/2 @ 11:59pm Project 1 due Thursday 2/5 @ 11:59pm 2 Announcements Homework 2 due Monday


slide-1
SLIDE 1

61A Lecture 6

Monday, February 2

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm

2

slide-4
SLIDE 4

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

2

slide-5
SLIDE 5

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB

2

slide-6
SLIDE 6

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall

2

slide-7
SLIDE 7

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall §Earn 1 bonus point if you finish by Wednesday 2/4 @ 11:59pm

2

slide-8
SLIDE 8

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall §Earn 1 bonus point if you finish by Wednesday 2/4 @ 11:59pm §Composition: Programs should be concise, well-named, understandable, and easy to follow

2

slide-9
SLIDE 9

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall §Earn 1 bonus point if you finish by Wednesday 2/4 @ 11:59pm §Composition: Programs should be concise, well-named, understandable, and easy to follow

  • Extra lecture 2 on Thursday 2/5 5pm-6:30pm in 2050 VLSB

2

slide-10
SLIDE 10

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall §Earn 1 bonus point if you finish by Wednesday 2/4 @ 11:59pm §Composition: Programs should be concise, well-named, understandable, and easy to follow

  • Extra lecture 2 on Thursday 2/5 5pm-6:30pm in 2050 VLSB

§Hog strategies & church numerals

2

slide-11
SLIDE 11

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall §Earn 1 bonus point if you finish by Wednesday 2/4 @ 11:59pm §Composition: Programs should be concise, well-named, understandable, and easy to follow

  • Extra lecture 2 on Thursday 2/5 5pm-6:30pm in 2050 VLSB

§Hog strategies & church numerals

  • Midterm 1 on Monday 2/9 7pm-9pm

2

slide-12
SLIDE 12

Announcements

  • Homework 2 due Monday 2/2 @ 11:59pm
  • Project 1 due Thursday 2/5 @ 11:59pm

§Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB §Partner party on Wednesday 2/4 3pm-4pm in Wozniak Lounge, Soda Hall §Earn 1 bonus point if you finish by Wednesday 2/4 @ 11:59pm §Composition: Programs should be concise, well-named, understandable, and easy to follow

  • Extra lecture 2 on Thursday 2/5 5pm-6:30pm in 2050 VLSB

§Hog strategies & church numerals

  • Midterm 1 on Monday 2/9 7pm-9pm

§Conflict? Fill out the conflict form today! http://goo.gl/2P5fKq

2

slide-13
SLIDE 13

Recursive Functions

slide-14
SLIDE 14

Recursive Functions

4

slide-15
SLIDE 15

Recursive Functions

Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly.

4

slide-16
SLIDE 16

Recursive Functions

Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly. Implication: Executing the body of a recursive function may require applying that function.

4

slide-17
SLIDE 17

Recursive Functions

Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly. Implication: Executing the body of a recursive function may require applying that function.

4

slide-18
SLIDE 18

Recursive Functions

Definition: A function is called recursive if the body of that function calls itself, either directly or indirectly. Implication: Executing the body of a recursive function may require applying that function.

Drawing Hands, by M. C. Escher (lithograph, 1948)

4

slide-19
SLIDE 19

Digit Sums

5

2+0+1+5 = 8

slide-20
SLIDE 20

Digit Sums

  • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9.

5

2+0+1+5 = 8

slide-21
SLIDE 21

Digit Sums

  • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9.
  • Useful for typo detection!

5

2+0+1+5 = 8

slide-22
SLIDE 22

Digit Sums

  • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9.
  • Useful for typo detection!

5

The Bank of 61A 1234 5678 9098 7658

OSKI THE BEAR

2+0+1+5 = 8

slide-23
SLIDE 23

Digit Sums

  • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9.
  • Useful for typo detection!

5

The Bank of 61A 1234 5678 9098 7658

OSKI THE BEAR

A checksum digit is a function of all the other digits; It can be computed to detect typos 2+0+1+5 = 8

slide-24
SLIDE 24

Digit Sums

  • If a number a is divisible by 9, then sum_digits(a) is also divisible by 9.
  • Useful for typo detection!

5

The Bank of 61A 1234 5678 9098 7658

OSKI THE BEAR

A checksum digit is a function of all the other digits; It can be computed to detect typos

  • Credit cards actually use the Luhn algorithm, which we'll implement after digit_sum.

2+0+1+5 = 8

slide-25
SLIDE 25

Sum Digits Without a While Statement

6

slide-26
SLIDE 26

Sum Digits Without a While Statement

6

def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10

slide-27
SLIDE 27

Sum Digits Without a While Statement

6

def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 def sum_digits(n): """Return the sum of the digits of positive integer n."""

slide-28
SLIDE 28

Sum Digits Without a While Statement

6

def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n

slide-29
SLIDE 29

Sum Digits Without a While Statement

6

def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n)

slide-30
SLIDE 30

Sum Digits Without a While Statement

6

def split(n): """Split positive n into all but its last digit and its last digit.""" return n // 10, n % 10 def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-31
SLIDE 31

The Anatomy of a Recursive Function

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-32
SLIDE 32

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-33
SLIDE 33

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-34
SLIDE 34

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-35
SLIDE 35

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-36
SLIDE 36

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases
  • Base cases are evaluated without recursive calls

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-37
SLIDE 37

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases
  • Base cases are evaluated without recursive calls

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-38
SLIDE 38

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases
  • Base cases are evaluated without recursive calls
  • Recursive cases are evaluated with recursive calls

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-39
SLIDE 39

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases
  • Base cases are evaluated without recursive calls
  • Recursive cases are evaluated with recursive calls

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-40
SLIDE 40

The Anatomy of a Recursive Function

  • The def statement header is similar to other functions
  • Conditional statements check for base cases
  • Base cases are evaluated without recursive calls
  • Recursive cases are evaluated with recursive calls

(Demo)

7

  • def sum_digits(n):

"""Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-41
SLIDE 41

Recursion in Environment Diagrams

slide-42
SLIDE 42

Recursion in Environment Diagrams

9

Interactive Diagram

slide-43
SLIDE 43

Recursion in Environment Diagrams

9

(Demo) Interactive Diagram

slide-44
SLIDE 44

Recursion in Environment Diagrams

9

(Demo) Interactive Diagram

slide-45
SLIDE 45

Recursion in Environment Diagrams

  • The same function fact is called

multiple times.

9

(Demo) Interactive Diagram

slide-46
SLIDE 46

Recursion in Environment Diagrams

  • The same function fact is called

multiple times.

9

(Demo) Interactive Diagram

slide-47
SLIDE 47

Recursion in Environment Diagrams

  • The same function fact is called

multiple times.

  • Different frames keep track of the

different arguments in each call.

9

(Demo) Interactive Diagram

slide-48
SLIDE 48

Recursion in Environment Diagrams

  • The same function fact is called

multiple times.

  • Different frames keep track of the

different arguments in each call.

  • What n evaluates to depends upon

which is the current environment.

9

(Demo) Interactive Diagram

slide-49
SLIDE 49

Recursion in Environment Diagrams

  • The same function fact is called

multiple times.

  • Different frames keep track of the

different arguments in each call.

  • What n evaluates to depends upon

which is the current environment.

9

(Demo) Interactive Diagram

slide-50
SLIDE 50

Recursion in Environment Diagrams

  • The same function fact is called

multiple times.

  • Different frames keep track of the

different arguments in each call.

  • What n evaluates to depends upon

which is the current environment.

  • Each call to fact solves a simpler

problem than the last: smaller n.

9

(Demo) Interactive Diagram

slide-51
SLIDE 51

Iteration vs Recursion

10

slide-52
SLIDE 52

Iteration vs Recursion

Iteration is a special case of recursion

10

slide-53
SLIDE 53

4! = 4 · 3 · 2 · 1 = 24

Iteration vs Recursion

Iteration is a special case of recursion

10

slide-54
SLIDE 54

4! = 4 · 3 · 2 · 1 = 24

Iteration vs Recursion

Iteration is a special case of recursion Using while:

10

slide-55
SLIDE 55

4! = 4 · 3 · 2 · 1 = 24

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total Using while:

10

slide-56
SLIDE 56

4! = 4 · 3 · 2 · 1 = 24

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total Using while: Using recursion:

10

slide-57
SLIDE 57

4! = 4 · 3 · 2 · 1 = 24

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion:

10

slide-58
SLIDE 58

4! = 4 · 3 · 2 · 1 = 24

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion: Math:

10

slide-59
SLIDE 59

4! = 4 · 3 · 2 · 1 = 24 n! =

n

Y

k=1

k

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion: Math:

10

slide-60
SLIDE 60

4! = 4 · 3 · 2 · 1 = 24 n! =

n

Y

k=1

k n! = ( 1 if n = 0 n · (n − 1)!

  • therwise

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion: Math:

10

slide-61
SLIDE 61

4! = 4 · 3 · 2 · 1 = 24 n! =

n

Y

k=1

k n! = ( 1 if n = 0 n · (n − 1)!

  • therwise

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion: Math: Names:

10

slide-62
SLIDE 62

4! = 4 · 3 · 2 · 1 = 24 n! =

n

Y

k=1

k n! = ( 1 if n = 0 n · (n − 1)!

  • therwise

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion:

n, total, k, fact_iter

Math: Names:

10

slide-63
SLIDE 63

4! = 4 · 3 · 2 · 1 = 24 n! =

n

Y

k=1

k n! = ( 1 if n = 0 n · (n − 1)!

  • therwise

Iteration vs Recursion

Iteration is a special case of recursion def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Using while: Using recursion:

n, total, k, fact_iter

Math: Names:

n, fact

10

slide-64
SLIDE 64

Verifying Recursive Functions

slide-65
SLIDE 65

The Recursive Leap of Faith

12

slide-66
SLIDE 66

The Recursive Leap of Faith

Photo by Kevin Lee, Preikestolen, Norway

12

slide-67
SLIDE 67

The Recursive Leap of Faith

Photo by Kevin Lee, Preikestolen, Norway

def fact(n): if n == 0: return 1 else: return n * fact(n-1)

12

slide-68
SLIDE 68

The Recursive Leap of Faith

Is fact implemented correctly?

Photo by Kevin Lee, Preikestolen, Norway

def fact(n): if n == 0: return 1 else: return n * fact(n-1)

12

slide-69
SLIDE 69

The Recursive Leap of Faith

Is fact implemented correctly? 1. Verify the base case.

Photo by Kevin Lee, Preikestolen, Norway

def fact(n): if n == 0: return 1 else: return n * fact(n-1)

12

slide-70
SLIDE 70

The Recursive Leap of Faith

Is fact implemented correctly? 1. Verify the base case. 2. Treat fact as a functional abstraction!

Photo by Kevin Lee, Preikestolen, Norway

def fact(n): if n == 0: return 1 else: return n * fact(n-1)

12

slide-71
SLIDE 71

The Recursive Leap of Faith

Is fact implemented correctly? 1. Verify the base case. 2. Treat fact as a functional abstraction! 3. Assume that fact(n-1) is correct.

Photo by Kevin Lee, Preikestolen, Norway

def fact(n): if n == 0: return 1 else: return n * fact(n-1)

12

slide-72
SLIDE 72

The Recursive Leap of Faith

Is fact implemented correctly? 1. Verify the base case. 2. Treat fact as a functional abstraction! 3. Assume that fact(n-1) is correct. 4. Verify that fact(n) is correct, assuming that fact(n-1) correct.

Photo by Kevin Lee, Preikestolen, Norway

def fact(n): if n == 0: return 1 else: return n * fact(n-1)

12

slide-73
SLIDE 73

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-74
SLIDE 74

The sum_digits function computes the sum of positive n correctly because:

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

slide-75
SLIDE 75

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n.

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case)

slide-76
SLIDE 76

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction)

slide-77
SLIDE 77

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k for all k with fewer digits than n,

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction) (simpler case)

slide-78
SLIDE 78

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k for all k with fewer digits than n, sum_digits(n) will be sum_digits(n//10) plus the last digit of n.

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction) (simpler case) (conclusion)

slide-79
SLIDE 79

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k for all k with fewer digits than n, sum_digits(n) will be sum_digits(n//10) plus the last digit of n.

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction) (simpler case) (conclusion)

slide-80
SLIDE 80

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k for all k with fewer digits than n, sum_digits(n) will be sum_digits(n//10) plus the last digit of n.

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction) (simpler case) (conclusion)

slide-81
SLIDE 81

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k for all k with fewer digits than n, sum_digits(n) will be sum_digits(n//10) plus the last digit of n.

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction) (simpler case) (conclusion)

slide-82
SLIDE 82

The sum_digits function computes the sum of positive n correctly because: The sum of the digits of any n < 10 is n. Assuming sum_digits(k) correctly sums the digits of k for all k with fewer digits than n, sum_digits(n) will be sum_digits(n//10) plus the last digit of n.

Verifying Digit Sum

13

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

(base case) (abstraction) (simpler case) (conclusion)

slide-83
SLIDE 83

Mutual Recursion

slide-84
SLIDE 84

The Luhn Algorithm

15

slide-85
SLIDE 85

The Luhn Algorithm

Used to verify credit card numbers

15

slide-86
SLIDE 86

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

15

slide-87
SLIDE 87

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

15

slide-88
SLIDE 88

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

  • Take the sum of all the digits.

15

slide-89
SLIDE 89

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

  • Take the sum of all the digits.

15

1 3 8 7 4 3

slide-90
SLIDE 90

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

  • Take the sum of all the digits.

15

1 3 8 7 4 3 2 3 1+6=7 7 8 3

slide-91
SLIDE 91

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

  • Take the sum of all the digits.

15

1 3 8 7 4 3 2 3 1+6=7 7 8 3 = 30

slide-92
SLIDE 92

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

  • Take the sum of all the digits.

15

1 3 8 7 4 3 2 3 1+6=7 7 8 3 The Luhn sum of a valid credit card number is a multiple of 10. = 30

slide-93
SLIDE 93

The Luhn Algorithm

Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

  • From the rightmost digit, which is the check digit, moving left, double the value of every

second digit; if product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

  • Take the sum of all the digits.

15

1 3 8 7 4 3 2 3 1+6=7 7 8 3 The Luhn sum of a valid credit card number is a multiple of 10. = 30 (Demo)

slide-94
SLIDE 94

Recursion and Iteration

slide-95
SLIDE 95

Converting Recursion to Iteration

17

slide-96
SLIDE 96

Converting Recursion to Iteration

Can be tricky: Iteration is a special case of recursion.

17

slide-97
SLIDE 97

Converting Recursion to Iteration

Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function.

17

slide-98
SLIDE 98

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

Converting Recursion to Iteration

Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function.

17

slide-99
SLIDE 99

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

Converting Recursion to Iteration

Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function.

17

What's left to sum

slide-100
SLIDE 100

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

Converting Recursion to Iteration

Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function. A partial sum

17

What's left to sum

slide-101
SLIDE 101

def sum_digits(n): """Return the sum of the digits of positive integer n.""" if n < 10: return n else: all_but_last, last = split(n) return sum_digits(all_but_last) + last

Converting Recursion to Iteration

Can be tricky: Iteration is a special case of recursion. Idea: Figure out what state must be maintained by the iterative function. A partial sum

17

(Demo) What's left to sum

slide-102
SLIDE 102

Converting Iteration to Recursion

18

slide-103
SLIDE 103

Converting Iteration to Recursion

More formulaic: Iteration is a special case of recursion.

18

slide-104
SLIDE 104

Converting Iteration to Recursion

More formulaic: Iteration is a special case of recursion. Idea: The state of an iteration can be passed as arguments.

18

slide-105
SLIDE 105

Converting Iteration to Recursion

More formulaic: Iteration is a special case of recursion. Idea: The state of an iteration can be passed as arguments. def sum_digits_iter(n): digit_sum = 0 while n > 0: n, last = split(n) digit_sum = digit_sum + last return digit_sum

18

slide-106
SLIDE 106

Converting Iteration to Recursion

More formulaic: Iteration is a special case of recursion. Idea: The state of an iteration can be passed as arguments. def sum_digits_iter(n): digit_sum = 0 while n > 0: n, last = split(n) digit_sum = digit_sum + last return digit_sum def sum_digits_rec(n, digit_sum): if n == 0: return digit_sum else: n, last = split(n) return sum_digits_rec(n, digit_sum + last)

18

slide-107
SLIDE 107

Converting Iteration to Recursion

More formulaic: Iteration is a special case of recursion. Idea: The state of an iteration can be passed as arguments. def sum_digits_iter(n): digit_sum = 0 while n > 0: n, last = split(n) digit_sum = digit_sum + last return digit_sum def sum_digits_rec(n, digit_sum): if n == 0: return digit_sum else: n, last = split(n) return sum_digits_rec(n, digit_sum + last) Updates via assignment become...

18

slide-108
SLIDE 108

Converting Iteration to Recursion

More formulaic: Iteration is a special case of recursion. Idea: The state of an iteration can be passed as arguments. def sum_digits_iter(n): digit_sum = 0 while n > 0: n, last = split(n) digit_sum = digit_sum + last return digit_sum def sum_digits_rec(n, digit_sum): if n == 0: return digit_sum else: n, last = split(n) return sum_digits_rec(n, digit_sum + last) Updates via assignment become... ...arguments to a recursive call

18