[2021 Spring] CS61A Discussion 5: Python Lists, Trees, Mutability

Discussion 5地址: https://inst.eecs.berkeley.edu/~cs61a/sp21/disc/disc05/#lists

Lists

Q1: Closest Number

要求一行写出,先用list(zip())获得nums列表中每个数字与目标值的差值 与它们在nums中的索引,求出最小差值和索引,根据索引求closest number。
尝试用min,发现只比较了每个元组的第一个元素,所以索引值是min(lt)[1].

lt = list(zip([abs(num - target) for num in nums], range(len(nums))))
index_min = min(lt)[1]
result = nums[index_min]
# Q1: Closest Number
def closest_number(nums, target):
    """
    >>> closest_number([1, 4, 5, 6, 7], 2)
    1
    >>> closest_number([3, 1, 5, 6, 13], 4) #  choose the earlier number since there's a tie
    3
    >>> closest_number([34, 102, 8, 5, 23], 25)
    23
    """
    return nums[min(list(zip([abs(num - target) for num in nums], range(len(nums)))))[1]]

Q2: (Tutorial) Max Product

# Q2: (Tutorial) Max Product
def max_product(s):
    """Return the maximum product that can be formed using non-consecutive
    elements of s.

    >>> max_product([10,3,1,9,2]) # 10 * 9
    90
    >>> max_product([5,10,5,10,5]) # 5 * 5 * 5
    125
    >>> max_product([])
    1
    """
    "*** YOUR CODE HERE ***"
    if s == []:
        return 1
    return max(max_product(s[1:]), s[0] * max_product(s[2:]))

Mutability

Q4: (Optional) Mystery Reverse Environment Diagram

def mystery(p, q):
    p[1].extend(q)
    q.append(p[1:])

p = [2, 3]
q = [4, [p]]
mystery(q, p)

Q5: (Tutorial) Add This Many

# Q5: (Tutorial) Add This Many
def add_this_many(x, el, s):
    """ Adds el to the end of s the number of times x occurs
    in s.
    >>> s = [1, 2, 4, 2, 1]
    >>> add_this_many(1, 5, s)
    >>> s
    [1, 2, 4, 2, 1, 5, 5]
    >>> add_this_many(2, 2, s)
    >>> s
    [1, 2, 4, 2, 1, 5, 5, 2, 2]
    """
    "*** YOUR CODE HERE ***"
    for i in s[:]:
        if i == x:
            s.append(el)

Trees

Q6: (Warmup) Height

# Q6: (Warmup) Height
def height(t):
    """Return the height of a tree.

    >>> t = tree(3, [tree(5, [tree(1)]), tree(2)])
    >>> height(t)
    2
    """
    "*** YOUR CODE HERE ***"
    if not children(t):
        return 1
    else:
        return max([height(children(c)) for c in children(t)]) + 1

Q7: Maximum Path Sum

# Q7: Maximum Path Sum
def max_path_sum(t):
    """Return the maximum path sum of the tree.

    >>> t = tree(1, [tree(5, [tree(1), tree(3)]), tree(10)])
    >>> max_path_sum(t)
    11
    """
    "*** YOUR CODE HERE ***"
    if is_leaf(t):
        return label(t)
    else:
        return max([max_path_sum(c) for c in children(t)]) + label(t)

Q8: (Tutorial) Find Path

# Q8: (Tutorial) Find Path
def find_path(tree, x):
    """
    >>> t = tree(2, [tree(7, [tree(3), tree(6, [tree(5), tree(11)])] ), tree(15)])
    >>> find_path(t, 5)
    [2, 7, 6, 5]
    >>> find_path(t, 10)  # returns None
    """
    if label(tree) == x:
        return [label(tree)]
    for c in children(tree):
        path = find_path(c, x)
        if path:
            return [label(tree)] + path

Tree - Implementation A

def tree(label, children=[]):
    return [label] + children

def label(tree):
    return tree[0]

def children(tree):
    return tree[1:]

def is_leaf(tree):
    return len(children(tree)) == 0
posted @ 2021-06-30 23:54  ikventure  阅读(984)  评论(0编辑  收藏  举报