
Week4 of CS61A
having two review class and two new class: 1. 9. Recursion 2. 10. Tree Recursion And HW 03
8. Midterm 1 Review Part 2
review
9. Recursion
talk about the recursion.
10. Tree Recursion
abstract recursion to a tree
recursion isn't a loop but breaking down big problems into smaller same ones


(6,4)
├── (2,4)
│ ├── (-2,4) ❌ 0
│ └── (2,3)
│ ├── (-1,3) ❌ 0
│ └── (2,2)
│ ├── (0,2) ✅ 1
│ └── (2,1)
│ ├── (1,1)
│ │ ├── (0,1) ✅ 1
│ │ └── (1,0) ❌ 0
│ └── (2,0) ❌ 0
│
└── (6,3)
├── (3,3)
│ ├── (0,3) ✅ 1
│ └── (3,2)
│
└── (6,2)
├── (4,2)
└── (6,1)
...difficulty of hw03
def interleaved_sum(n, f_odd, f_even):
"""Compute the sum f_odd(1) + f_even(2) + f_odd(3) + ..., up
to n.
>>> identity = lambda x: x
>>> square = lambda x: x * x
>>> triple = lambda x: x * 3
>>> interleaved_sum(5, identity, square) # 1 + 2*2 + 3 + 4*4 + 5
29
>>> interleaved_sum(5, square, identity) # 1*1 + 2 + 3*3 + 4 + 5*5
41
>>> interleaved_sum(4, triple, square) # 1*3 + 2*2 + 3*3 + 4*4
32
>>> interleaved_sum(4, square, triple) # 1*1 + 2*3 + 3*3 + 4*3
28
>>> from construct_check import check
>>> check(SOURCE_FILE, 'interleaved_sum', ['While', 'For', 'Mod']) # ban loops and %
True
>>> check(SOURCE_FILE, 'interleaved_sum', ['BitAnd', 'BitOr', 'BitXor']) # ban bitwise operators, don't worry about these if you don't know what they are
True
"""
"*** YOUR CODE HERE ***"
def helper(i, odd):
if i > n:
return 0
return (f_odd if odd else f_even)(i) + helper(i + 1, not odd)def next_smaller_dollar(bill):
"""Returns the next smaller bill in order."""
if bill == 100:
return 50
if bill == 50:
return 20
if bill == 20:
return 10
elif bill == 10:
return 5
elif bill == 5:
return 1
def count_dollars(total):
"""Return the number of ways to make change.
>>> count_dollars(15) # 15 $1 bills, 10 $1 & 1 $5 bills, ... 1 $5 & 1 $10 bills
6
>>> count_dollars(10) # 10 $1 bills, 5 $1 & 1 $5 bills, 2 $5 bills, 10 $1 bills
4
>>> count_dollars(20) # 20 $1 bills, 15 $1 & $5 bills, ... 1 $20 bill
10
>>> count_dollars(45) # How many ways to make change for 45 dollars?
44
>>> count_dollars(100) # How many ways to make change for 100 dollars?
344
>>> count_dollars(200) # How many ways to make change for 200 dollars?
3274
>>> from construct_check import check
>>> # ban iteration
>>> check(SOURCE_FILE, 'count_dollars', ['While', 'For'])
True
"""
"*** YOUR CODE HERE ***"
def helper(amount, bill):
if amount == 0:
return 1
if amount < 0 or bill is None:
return 0
return (
helper(amount - bill, bill)
+ helper(amount, next_smaller_dollar(bill))
)
return helper(total, 100)def print_move(origin, destination):
"""Print instructions to move a disk."""
print("Move the top disk from rod", origin, "to rod", destination)
def move_stack(n, start, end):
"""Print the moves required to move n disks on the start pole to the end
pole without violating the rules of Towers of Hanoi.
n -- number of disks
start -- a pole position, either 1, 2, or 3
end -- a pole position, either 1, 2, or 3
There are exactly three poles, and start and end must be different. Assume
that the start pole has at least n disks of increasing size, and the end
pole is either empty or has a top disk larger than the top n start disks.
>>> move_stack(1, 1, 3)
Move the top disk from rod 1 to rod 3
>>> move_stack(2, 1, 3)
Move the top disk from rod 1 to rod 2
Move the top disk from rod 1 to rod 3
Move the top disk from rod 2 to rod 3
>>> move_stack(3, 1, 3)
Move the top disk from rod 1 to rod 3
Move the top disk from rod 1 to rod 2
Move the top disk from rod 3 to rod 2
Move the top disk from rod 1 to rod 3
Move the top disk from rod 2 to rod 1
Move the top disk from rod 2 to rod 3
Move the top disk from rod 1 to rod 3
"""
assert 1 <= start <= 3 and 1 <= end <= 3 and start != end, "Bad start/end"
"*** YOUR CODE HERE ***"
if n == 1:
print_move(start, end)
else:
other = 6 - (start + end)
move_stack(n - 1, start, other)
print_move(start, end)
move_stack(n - 1, other, end)from operator import sub, mul
def make_anonymous_factorial():
"""Return the value of an expression that computes factorial.
>>> make_anonymous_factorial()(5)
120
>>> from construct_check import check
>>> # ban any assignments or recursion
>>> check(SOURCE_FILE, 'make_anonymous_factorial',
... ['Assign', 'AnnAssign', 'AugAssign', 'NamedExpr', 'FunctionDef', 'Recursion'])
True
"""
return (lambda f: (lambda n: f(f, n)))(
lambda f, n: 1 if n == 0 else mul(n, f(f, sub(n, 1)))
)