Skip to content

Latest commit

 

History

History
executable file
·
71 lines (61 loc) · 1.87 KB

project-0.md

File metadata and controls

executable file
·
71 lines (61 loc) · 1.87 KB
tags comments dg-publish
notes
true
true

Tutorial

[!PREREQUISITE]

explain

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

python autograder.py # 评分全部
python autograder.py -q q1 # 测试第一问
def add(a, b):
    "Return the sum of a and b"
    "*** YOUR CODE HERE ***"
    return a + b
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
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