CS61A Lecture 9
Transcript of CS61A Lecture 9
![Page 1: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/1.jpg)
CS61A Lecture 9
Amir KamilUC Berkeley
February 11, 2013
![Page 2: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/2.jpg)
HW3 due Tuesday at 7pm
Hog due today! Hog contest due later; see announcement tonight
Midterm Wednesday at 7pm See course website for assigned locations, more info
Midterm review in lab this week
Announcements
![Page 3: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/3.jpg)
Factorial
![Page 4: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/4.jpg)
The factorial of a non‐negative integer n is
Factorial
![Page 5: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/5.jpg)
The factorial of a non‐negative integer n is
Factorial
![Page 6: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/6.jpg)
The factorial of a non‐negative integer n is
Factorial
![Page 7: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/7.jpg)
The factorial of a non‐negative integer n is
Factorial
![Page 8: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/8.jpg)
The factorial of a non‐negative integer n is
Factorial
![Page 9: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/9.jpg)
The factorial of a non‐negative integer n is
This is called a recurrence relation;
Factorial
![Page 10: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/10.jpg)
The factorial of a non‐negative integer n is
This is called a recurrence relation;Factorial is defined in terms of itself
Factorial
![Page 11: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/11.jpg)
The factorial of a non‐negative integer n is
This is called a recurrence relation;Factorial is defined in terms of itselfCan we write code to compute factorial using the same pattern?
Factorial
![Page 12: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/12.jpg)
Computing Factorial
![Page 13: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/13.jpg)
We can compute factorial using the direct definition
Computing Factorial
![Page 14: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/14.jpg)
We can compute factorial using the direct definition
Computing Factorial
![Page 15: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/15.jpg)
We can compute factorial using the direct definition
Computing Factorial
def factorial_iter(n):if n == 0 or n == 1:
return 1total = 1while n >= 1:
total, n = total * n, n - 1return total
![Page 16: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/16.jpg)
Computing Factorial
![Page 17: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/17.jpg)
Can we compute it using the recurrence relation?
Computing Factorial
![Page 18: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/18.jpg)
Can we compute it using the recurrence relation?
Computing Factorial
![Page 19: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/19.jpg)
Can we compute it using the recurrence relation?
Computing Factorial
def factorial(n):if n == 0 or n == 1:
return 1return n * factorial(n - 1)
![Page 20: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/20.jpg)
Can we compute it using the recurrence relation?
Computing Factorial
def factorial(n):if n == 0 or n == 1:
return 1return n * factorial(n - 1)
![Page 21: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/21.jpg)
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 1return n * factorial(n - 1)
![Page 22: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/22.jpg)
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
![Page 23: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/23.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
![Page 24: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/24.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
![Page 25: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/25.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
![Page 26: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/26.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
![Page 27: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/27.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
![Page 28: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/28.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
Compute 2!
![Page 29: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/29.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
Compute 2!
Compute 1!
![Page 30: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/30.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
Compute 2!
Compute 1!
![Page 31: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/31.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
Compute 2!
Compute 1!
![Page 32: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/32.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
Compute 2!
Compute 1!
![Page 33: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/33.jpg)
Let’s see what happens!
Factorial Environment Diagram
Example: http://goo.gl/NjCKG
Compute 4!
Compute 3!
Compute 2!
Compute 1!
![Page 34: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/34.jpg)
Recursive Functions
def factorial(n):if n == 0 or n == 1:
return 1return n * factorial(n - 1)
![Page 35: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/35.jpg)
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 1return n * factorial(n - 1)
![Page 36: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/36.jpg)
A function is recursive if the body calls the function itself, either directly or indirectly
Recursive functions have two important components:ion itself, either directly or indirectly
Recursive Functions
def factorial(n):if n == 0 or n == 1:
return 1return n * factorial(n - 1)
![Page 37: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/37.jpg)
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 1return n * factorial(n - 1)
![Page 38: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/38.jpg)
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 1return n * factorial(n - 1)
Base case
![Page 39: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/39.jpg)
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 itself2. 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 1return n * factorial(n - 1)
Base case
![Page 40: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/40.jpg)
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 itself2. 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 1return n * factorial(n - 1)
Recursive case
Base case
![Page 41: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/41.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
![Page 42: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/42.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 43: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/43.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 44: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/44.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 45: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/45.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 46: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/46.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 47: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/47.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 48: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/48.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 49: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/49.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 50: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/50.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 51: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/51.jpg)
Recursion Example: Heavy Box
a
Heavy Book Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 52: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/52.jpg)
Recursion Example: Heavy Box
a
Heavy Book Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 53: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/53.jpg)
Recursion Example: Heavy Box
a
Heavy Book Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 54: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/54.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 55: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/55.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 56: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/56.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 57: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/57.jpg)
Recursion Example: Heavy Box
Heavy Book
Heavy Book
Heavy Book
Heavy Book
adef lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 58: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/58.jpg)
Recursion Example: Heavy Box
Heavy Book
Heavy Book
Heavy Book
a
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 59: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/59.jpg)
Recursion Example: Heavy Box
Heavy Book
Heavy Book
a
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 60: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/60.jpg)
Recursion Example: Heavy Box
Heavy Book
a
Heavy Book
Heavy Book
Heavy Book
def lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 61: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/61.jpg)
Recursion Example: Heavy Box
a
Heavy Book
Heavy Book
Heavy Book
Heavy Bookdef lift_box(box):if too_heavy(box):
book = remove_book(box)lift_box(box)add_book(box, book)
else:move_box(box)
![Page 62: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/62.jpg)
Recursion Example: Duplication
![Page 63: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/63.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 64: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/64.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 65: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/65.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 66: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/66.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 67: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/67.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 68: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/68.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 69: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/69.jpg)
Recursion Example: Duplication
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 70: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/70.jpg)
Recursion Example: Duplication
No base case!
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 71: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/71.jpg)
Recursion Example: Duplication
Futurama Season 6, Episode 17 “Benderama”
© Twentieth Century Fox Film Corporation
No base case!
def duplicate(size):return (duplicate(0.6 * size) +
duplicate(0.6 * size))
![Page 72: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/72.jpg)
Recursion Example: Dreaming
![Page 73: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/73.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
![Page 74: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/74.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
![Page 75: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/75.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global frame
![Page 76: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/76.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global frame dream func dream(level)
![Page 77: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/77.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global frame dream func dream(level)
![Page 78: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/78.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream func dream(level)
![Page 79: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/79.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream level 1
dream func dream(level)
![Page 80: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/80.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream level 1
dream func dream(level)
![Page 81: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/81.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream level 1
dream func dream(level)
![Page 82: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/82.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
level 1dream func dream(level)
![Page 83: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/83.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
level 1
level 2
dream func dream(level)
![Page 84: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/84.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
level 1
level 2
dream func dream(level)
![Page 85: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/85.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
level 1
level 2
dream func dream(level)
![Page 86: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/86.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
dream
level 1
level 2
dream func dream(level)
![Page 87: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/87.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
dream
level 1
level 2
level 3
dream func dream(level)
![Page 88: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/88.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
dream
level 1
level 2
level 3
dream func dream(level)
![Page 89: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/89.jpg)
Recursion Example: Dreaming
def dream(level):if level == 3:return inception()
else:return dream(level + 1)
Global framedream
dream
dream
level 1
level 2
level 3
Inception© Warner Brothers Entertainment
dream func dream(level)
![Page 90: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/90.jpg)
Reversing the Order of Recursive Calls
![Page 91: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/91.jpg)
Some recursive computations may be done more easily by reversing the order of recursive calls.
Reversing the Order of Recursive Calls
![Page 92: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/92.jpg)
Some recursive computations may be done more easily by reversing the order of recursive calls.A helper function helps us to do this.
Reversing the Order of Recursive Calls
![Page 93: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/93.jpg)
Some recursive computations may be done more easily by reversing the order of recursive calls.A helper function helps us to do this.
Reversing the Order of Recursive Calls
![Page 94: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/94.jpg)
Some recursive computations may be done more easily by reversing the order of recursive calls.A helper function helps us to do this.
Reversing the Order of Recursive Calls
def factorial2(n):return factorial_helper(n, 1)
def factorial_helper(n, k):if k >= n:
return kreturn k * factorial_helper(n, k + 1)
![Page 95: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/95.jpg)
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
![Page 96: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/96.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
![Page 97: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/97.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
![Page 98: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/98.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
![Page 99: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/99.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
![Page 100: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/100.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
![Page 101: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/101.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
Compute 3*2
![Page 102: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/102.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
Compute 3*2
Compute 3
![Page 103: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/103.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
Compute 3*2
Compute 3
![Page 104: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/104.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
Compute 3*2
Compute 3
![Page 105: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/105.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
Compute 3*2
Compute 3
![Page 106: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/106.jpg)
Here is how the reversedcomputation evolves
Reverse Environment Diagram
Example: http://goo.gl/6zz0z
Compute 3!
Compute 3*2*1
Compute 3*2
Compute 3
![Page 107: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/107.jpg)
Fibonacci Sequence
Example: http://goo.gl/9UJxG
![Page 108: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/108.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
Example: http://goo.gl/9UJxG
![Page 109: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/109.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
Example: http://goo.gl/9UJxG
![Page 110: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/110.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
def fib_iter(n):if n == 0:
return 0fib_n, fib_n_1 = 1, 0k = 1while k < n:
fib_n, fib_n_1 = fib_n_1 + fib_n, fib_nk += 1
return fib_nExample: http://goo.gl/9UJxG
![Page 111: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/111.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
Example: http://goo.gl/DZbRG
![Page 112: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/112.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
def fib(n):if n == 0:
return 0elif n == 1:
return 1return fib(n - 1) + fib(n - 2)
Example: http://goo.gl/DZbRG
![Page 113: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/113.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
def fib(n):if n == 0:
return 0elif n == 1:
return 1return fib(n - 1) + fib(n - 2)
Example: http://goo.gl/DZbRG
![Page 114: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/114.jpg)
The Fibonacci sequence is defined as
Fibonacci Sequence
def fib(n):if n == 0:
return 0elif n == 1:
return 1return fib(n - 1) + fib(n - 2)
Two recursive calls!
Example: http://goo.gl/DZbRG
![Page 115: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/115.jpg)
Tree recursion
![Page 116: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/116.jpg)
Executing the body of a function may entail more than one recursive call to that function
Tree recursion
![Page 117: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/117.jpg)
Executing the body of a function may entail more than one recursive call to that functionThis is called tree recursion
Tree recursion
![Page 118: CS61A Lecture 9](https://reader031.fdocuments.in/reader031/viewer/2022012500/61789a487d8a5a7bb5194217/html5/thumbnails/118.jpg)
Executing the body of a function may entail more than one recursive call to that functionThis is called tree recursion
Tree recursion
fib(5)
fib(4)
fib(3)
fib(1)
1
fib(2)
fib(0)fib(1)
01
fib(2)
fib(0)fib(1)
01
fib(3)
fib(1)
1
fib(2)
fib(0)fib(1)
01