### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# CS 61A Lecture 6 CS 61A

CAL

### View Full Document

## About this Document

## 3

## 0

## Popular in The Structure and Interpretation of Computer Programs

## Popular in Elect Engr & Computer Science

This 16 page Class Notes was uploaded by Scott Lee on Thursday September 8, 2016. The Class Notes belongs to CS 61A at University of California Berkeley taught by John DeNero in Fall 2016. Since its upload, it has received 3 views. For similar materials see The Structure and Interpretation of Computer Programs in Elect Engr & Computer Science at University of California Berkeley.

## Reviews for CS 61A Lecture 6

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/08/16

61A Lecture 6 Announcements Recursive Functions Recursive Functions 4 Recursive Functions Deﬁnition: A function is called recursive if the body of that function calls itself, either directly or indirectly 4 Recursive Functions Deﬁnition: 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 Recursive Functions Deﬁnition: 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 Recursive Functions Deﬁnition: 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 Digit Sums 5 2+0+1+5 = 8 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 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 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 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 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 Sum Digits Without a While Statement 6 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 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.""" 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 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) 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 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 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 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 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 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 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 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 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 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 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 Recursion in Environment Diagrams Recursion in Environment Diagrams 9 Interactive Diagram Recursion in Environment Diagrams 9 (Demo) Interactive Diagram Recursion in Environment Diagrams 9 (Demo) Interactive Diagram Recursion in Environment Diagrams • The same function fact is called multiple times 9 (Demo) Interactive Diagram Recursion in Environment Diagrams • The same function fact is called multiple times 9 (Demo) Interactive Diagram 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 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 the current environment 9 (Demo) Interactive Diagram 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 the current environment 9 (Demo) Interactive Diagram 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 the current environment • Each call to fact solves a simpler problem than the last: smaller n 9 (Demo) Interactive Diagram Iteration vs Recursion 10 Iteration vs Recursion Iteration is a special case of recursion 10 4! = 4 · 3 · 2 · 1 = 24 Iteration vs Recursion Iteration is a special case of recursion 10 4! = 4 · 3 · 2 · 1 = 24 Iteration vs Recursion Iteration is a special case of recursion Using while: 10 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 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 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 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 4! = 4 · 3 · 2 · 1 = 24 n! = Yn 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 4! = 4 · 3 · 2 · 1 = 24 n! = Yn k=1 k n! = ( 1 if n = 0 n · (n ! 1)! otherwise 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 4! = 4 · 3 · 2 · 1 = 24 n! = Yn k=1 k n! = ( 1 if n = 0 n · (n ! 1)! otherwise 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 4! = 4 · 3 · 2 · 1 = 24 n! = Yn k=1 k n! = ( 1 if n = 0 n · (n ! 1)! otherwise 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 4! = 4 · 3 · 2 · 1 = 24 n! = Yn k=1 k n! = ( 1 if n = 0 n · (n ! 1)! otherwise 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 Verifying Recursive Functions The Recursive Leap of Faith 12 The Recursive Leap of Faith Photo by Kevin Lee, Preikestolen, Norway 12 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 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 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 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 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 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 Photo by Kevin Lee, Preikestolen, Norway def fact(n): if n == 0: return 1 else: return n * fact(n-1) 12 Mutual Recursion The Luhn Algorithm 14 The Luhn Algorithm Used to verify credit card numbers 14 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm 14 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) 14 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) • Second: Take the sum of all the digits 14 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) • Second: Take the sum of all the digits 14 1 3 8 7 4 3 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) • Second: Take the sum of all the digits 14 1 3 8 7 4 3 2 3 1+6=7 7 8 3 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) • Second: Take the sum of all the digits 14 1 3 8 7 4 3 2 3 1+6=7 7 8 3 = 30 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) • Second: Take the sum of all the digits 14 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 The Luhn Algorithm Used to verify credit card numbers From Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm • First: 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) • Second: Take the sum of all the digits 14 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) Recursion and Iteration Converting Recursion to Iteration 16 Converting Recursion to Iteration Can be tricky: Iteration is a special case of recursion. 16 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. 16 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. 16 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. 16 What's left to sum 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 16 What's left to sum 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 16 (Demo) What's left to sum Converting Iteration to Recursion 17 Converting Iteration to Recursion More formulaic: Iteration is a special case of recursion. 17 Converting Iteration to Recursion More formulaic: Iteration is a special case of recursion. Idea: The state of an iteration can be passed as arguments. 17 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 17 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) 17 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... 17 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 17

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

#### "I made $350 in just two days after posting my first study guide."

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

#### "Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.