跳转至

project-0

[!PREREQUISITE]

explain

这个是让我们熟悉项目使用和 python 的考察,没啥好讲的。

python autograder.py # 评分全部
python autograder.py -q q1 # 测试第一问
addition.py
def add(a, b):
    "Return the sum of a and b"
    "*** YOUR CODE HERE ***"
    return a + b
buyLotsOfFruit.py
def buyLotsOfFruit(orderList):
    """
        orderList: List of (fruit, numPounds) tuples

    Returns cost of order
    """
    totalCost = 0.0
    "*** YOUR CODE HERE ***"
    # 对于这道简单的题,下面的条件语句倒是可以省略
    for fruit, numPounds in orderList:
        if fruit in fruitPrices:
            totalCost += fruitPrices[fruit] * numPounds
        else:
            print("Sorry we don't have %s" % fruit)
            return None
    return totalCost
shopSmart.py
def shopSmart(orderList, fruitShops):
    """
    orderList: List of (fruit, numPound) tuples
    fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    best_shop = None
    # 将 lowest_cost 初始化为正无穷大,这样第一次循环时,lowest_cost 会被更新为第一个商店的 cost
    lowest_cost = float("inf")
    for fruitShop in fruitShops:
        cost = 0
        for fruit, numPounds in orderList:
            cost += fruitShop.fruitPrices[fruit] * numPounds
        if cost < lowest_cost:
            lowest_cost = cost
            best_shop = fruitShop
    return best_shop

pass

评论