Java教程

[2021 spring] CS61A Lab 5: Python Lists, Data Abstraction, Trees

本文主要是介绍[2021 spring] CS61A Lab 5: Python Lists, Data Abstraction, Trees,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

lab05: https://inst.eecs.berkeley.edu/~cs61a/sp21/lab/lab05/#topics
lab5包括对列表的理解,数据抽象,和树

目录
  • Topics
    • List Comprehensions
    • Data Abstraction
    • Trees
  • Required Questions
    • Q1: Couple(List Comprehensions)
    • Q2: Distance(Data Abstraction)
    • Q3: Closer city(Data Abstraction)
    • Q5: Finding Berries!(Trees)
    • Q6: Sprout leaves(Trees)

Topics

List Comprehensions

Data Abstraction

Trees



Required Questions

Q1: Couple(List Comprehensions)

def couple(s, t):
    """Return a list of two-element lists in which the i-th element is [s[i], t[i]].

    >>> a = [1, 2, 3]
    >>> b = [4, 5, 6]
    >>> couple(a, b)
    [[1, 4], [2, 5], [3, 6]]
    >>> c = ['c', 6]
    >>> d = ['s', '1']
    >>> couple(c, d)
    [['c', 's'], [6, '1']]
    """
    assert len(s) == len(t)
    "*** YOUR CODE HERE ***"
    return [[s[i], t[i]] for i in range(len(s))]

Q2: Distance(Data Abstraction)

from math import sqrt


def distance(city_a, city_b):
    """
    >>> city_a = make_city('city_a', 0, 1)
    >>> city_b = make_city('city_b', 0, 2)
    >>> distance(city_a, city_b)
    1.0
    >>> city_c = make_city('city_c', 6.5, 12)
    >>> city_d = make_city('city_d', 2.5, 15)
    >>> distance(city_c, city_d)
    5.0
    """
    "*** YOUR CODE HERE ***"
    return sqrt((get_lat(city_a) - get_lat(city_b)) ** 2 +\
        (get_lon(city_a) - get_lon(city_b)) ** 2)

Q3: Closer city(Data Abstraction)

def closer_city(lat, lon, city_a, city_b):
    """
    Returns the name of either city_a or city_b, whichever is closest to
    coordinate (lat, lon). If the two cities are the same distance away
    from the coordinate, consider city_b to be the closer city.

    >>> berkeley = make_city('Berkeley', 37.87, 112.26)
    >>> stanford = make_city('Stanford', 34.05, 118.25)
    >>> closer_city(38.33, 121.44, berkeley, stanford)
    'Stanford'
    >>> bucharest = make_city('Bucharest', 44.43, 26.10)
    >>> vienna = make_city('Vienna', 48.20, 16.37)
    >>> closer_city(41.29, 174.78, bucharest, vienna)
    'Bucharest'
    """
    "*** YOUR CODE HERE ***"
    target = make_city('target', lat, lon)
    dist_a = distance(target, city_a)
    dist_b = distance(target, city_b)
    return get_name(city_a) if dist_a <= dist_b else get_name(city_b)

Q5: Finding Berries!(Trees)

def berry_finder(t):
    """Returns True if t contains a node with the value 'berry' and 
    False otherwise.

    >>> scrat = tree('berry')
    >>> berry_finder(scrat)
    True
    >>> sproul = tree('roots', [tree('branch1', [tree('leaf'), tree('berry')]), tree('branch2')])
    >>> berry_finder(sproul)
    True
    >>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
    >>> berry_finder(numbers)
    False
    >>> t = tree(1, [tree('berry',[tree('not berry')])])
    >>> berry_finder(t)
    True
    """
    "*** YOUR CODE HERE ***"
    if label(t) == 'berry': return True
    for c in branches(t):
        if berry_finder(c):
            return True
    return False

Q6: Sprout leaves(Trees)


def sprout_leaves(t, leaves):
    """Sprout new leaves containing the data in leaves at each leaf in
    the original tree t and return the resulting tree.

    >>> t1 = tree(1, [tree(2), tree(3)])
    >>> print_tree(t1)
    1
      2
      3
    >>> new1 = sprout_leaves(t1, [4, 5])
    >>> print_tree(new1)
    1
      2
        4
        5
      3
        4
        5

    >>> t2 = tree(1, [tree(2, [tree(3)])])
    >>> print_tree(t2)
    1
      2
        3
    >>> new2 = sprout_leaves(t2, [6, 1, 2])
    >>> print_tree(new2)
    1
      2
        3
          6
          1
          2
    """
    "*** YOUR CODE HERE ***"
    if is_leaf(t):
        return tree(label(t), [tree(i) for i in leaves])
    return tree(label(t), [sprout_leaves(c, leaves) for c in branches(t)])
这篇关于[2021 spring] CS61A Lab 5: Python Lists, Data Abstraction, Trees的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!