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

Posted ikventure

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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


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

以上是关于[2021 Spring] CS61A Discussion 5: Python Lists, Trees, Mutability的主要内容,如果未能解决你的问题,请参考以下文章

[2021 Spring] CS61A 学习笔记 lecture 10 containers and sequences

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

[2021 spring] CS61A Lab 2: Higher-Order Functions, Lambda Expressions

[2021 Spring] CS61A Project 1: The Game of Hog (Phase 3)

CS61A学习笔记 lecture1 Computer science

CS61A 2022 fall lab01