Week7 of CS61A

Last updated on

3 lectures, 1 lab, 1disc, 1 hw, 1 pj 1. lecture 1. 17. Generators 2. 18. Objects 3. 19. Attributes 2. lab 1. lab 04 3. disc 1. disc 06 4. hw 1. hw 04 5. pj 1. Ants


17. Generators

2 point : Generator and Tree practice image

image

def park(n):
    if n == 0:
        yield ""
    else:
        for way in park(n - 1):
            yield "." + way
            yield "%" + way

        if n >= 2:
            for way in park(n - 2):
                yield "<>" + way

![[Pasted image 20260905101057.png]]

def exclude(t, x):
    filtered_branches = map(lambda y: exclude(y, x), branches(t))
    bs = []

    for b in filtered_branches:
        if label(b) == x:
            bs.extend(branches(b))
        else:
            bs.append(b)

    return tree(label(t), bs)

the difficult point of lab05

def sum_tree(t):
"""Add all elements in a tree.
>>> t = tree(4, [tree(2, [tree(3)]), tree(6)])
>>> sum_tree(t)
15
"""
"*** YOUR CODE HERE ***"
# if is_leaf(t):
#     return label(t)
# result = label(t)
# for b in branches(t):
#     result = sum_tree(b) + result
# return result
total = 0
for b in branches(t):
	total += sum_tree(b)
return label(t) + total


def balanced(t):
"""Checks if each branch has same sum of all elements and
if each branch is balanced.
>>> t = tree(1, [tree(3), tree(1, [tree(2)]), tree(1, [tree(1), tree(1)])])
>>> balanced(t)
True
>>> t = tree(1, [t, tree(1)])
>>> balanced(t)
False
>>> t = tree(1, [tree(4), tree(1, [tree(2), tree(1)]), tree(1, [tree(3)])])
>>> balanced(t)
False
"""
"*** YOUR CODE HERE ***"

	if is_leaf(t):
	    return True
	sums = [sum_tree(b) for b in branches(t)]
	same_sum = all(s == sums[0] for s in sums)
	all_balanced = all(balanced(b) for b in branches(t))
	
	return same_sum and all_balanced

18. Objects

Learning the Object of Python.

The difficult point of disc06.

def gen_fib():
	n, add = 0, 1
	while True:
		yield n
		n, add = n + add, n

  
next(filter(lambda n: n > 2025, gen_fib()))
def partition_gen(n, m):
"""Yield the partitions of n using parts up to size m.
>>> for partition in sorted(partition_gen(6, 4)):
...     print(partition)
1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 2
1 + 1 + 1 + 3
1 + 1 + 2 + 2
1 + 1 + 4
1 + 2 + 3
2 + 2 + 2
2 + 4
3 + 3
"""
	assert n > 0 and m > 0
	if n == m:
		yield str(m)
	if n - m > 0:
	"*** YOUR CODE HERE ***"
		for p in partition_gen(n - m, m): 
			yield p + " + " + str(m)
	if m > 1: 
	"*** YOUR CODE HERE ***"
		yield from partition_gen(n, m - 1)
def squares(total, k):

"""Yield the ways in which perfect squares greater or equal to k*k sum to total.
>>> list(squares(10, 1)) # All lists of perfect squares that sum to 10
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 1, 1, 1, 1, 1, 1], [4, 4, 1, 1], [9, 1]]
>>> list(squares(20, 2)) # Only use perfect squares greater or equal to 4 (2*2).
[[4, 4, 4, 4, 4], [16, 4]]
"""
	assert total > 0 and k > 0
	if total == k * k:
		yield [k * k]
	elif total > k * k:
		for s in squares(total - k * k, k):
			yield [k * k] + s
		yield from squares(total, k + 1)

19. Attributes

Learning something about the attribute of class in python.

And the hw05 and ant is too simple, I don't to plan to record it.