cs188 project0实验

博主 3043 2021-09-22

高级人工智能-project 0 实验报告

实验内容:

指引链接:https://inst.eecs.berkeley.edu/~cs188/sp21/project0/

Question 1: Addition

Open addition.py and look at the definition of add:

def add(a, b):
    "Return the sum of a and b"
    "*** YOUR CODE HERE ***"
    return 0

打开并完善add(a,b)方法,实现两个数的加法,并返回和。完善代码如下:

def add(a, b):
    "Return the sum of a and b"
    return (a) + (b)

Question 2: buyLotsOfFruit function

Implement the buyLotsOfFruit(orderList) function in buyLotsOfFruit.py which takes a list of (fruit,numPounds) tuples and returns the cost of your list. If there is some fruit in the list which doesn’t appear in fruitPrices it should print an error message and return None. Please do not change the fruitPrices variable.

buyLotsOfFruit.py文件中已给出的部分代码如下:

from __future__ import print_function

fruitPrices = {'apples': 2.00, 'oranges': 1.50, 'pears': 1.75,
               'limes': 0.75, 'strawberries': 1.00}


def buyLotsOfFruit(orderList):
    """
        orderList: List of (fruit, numPounds) tuples

    Returns cost of order
    """
    totalCost = 0.0
    "*** YOUR CODE HERE ***"
    return totalCost


# Main Method
if __name__ == '__main__':
    "This code runs when you invoke the script from the command line"
    orderList = [('apples', 2.0), ('pears', 3.0), ('limes', 4.0)]
    print('Cost of', orderList, 'is', buyLotsOfFruit(orderList))

solution: 遍历给出的orderList,取出每个需要购买的水果名字name和数量weight,查询价格表price,计算各个水果的price*weight,并将值加到totalCost上
补充buyLotsOfFruit(orderList)函数如下:

def buyLotsOfFruit(orderList):
    """
        orderList: List of (fruit, numPounds) tuples

    Returns cost of order
    """
    totalCost = 0.0
    for fruit in orderList:
        name = fruit[0]
        if name not in fruitPrices:
            print(name + ' is not in the price list!')
            return None
        totalCost += fruitPrices[name] * fruit[1]
    return totalCost

Question 3:shopSmart function

Fill in the function shopSmart(orderList,fruitShops) in shopSmart.py, which takes an orderList (like the kind passed in to FruitShop.getPriceOfOrder) and a list of FruitShop and returns the FruitShop where your order costs the least amount in total. Don’t change the file name or variable names, please. Note that we will provide the shop.py implementation as a “support” file, so you don’t need to submit yours.

shopSmart.py中已给出的部分代码如下:

from __future__ import print_function
import shop


def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    return None


if __name__ == '__main__':
    "This code runs when you invoke the script from the command line"
    orders = [('apples', 1.0), ('oranges', 3.0)]
    dir1 = {'apples': 2.0, 'oranges': 1.0}
    shop1 = shop.FruitShop('shop1', dir1)
    dir2 = {'apples': 1.0, 'oranges': 5.0}
    shop2 = shop.FruitShop('shop2', dir2)
    shops = [shop1, shop2]
    print("For orders ", orders, ", the best shop is", shopSmart(orders, shops).getName())
    orders = [('apples', 3.0)]
    print("For orders: ", orders, ", the best shop is", shopSmart(orders, shops).getName())

solution: 设定一个全局最小费用,遍历各个商店,通过给出的水果订单数量,计算在该商店购买水果需要花费的费用,如果费用比最小费用小,记录该商店,并更新最小费用值。最后返回记录的商店即可。
补充代码如下:

def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    
    leastCost = 0xFFFFFFFF
    if len(fruitShops) == 0:
        return None
    chosenShop = fruitShops[0]
    for shop in fruitShops:
        curShopCost = shop.getPriceOfOrder(orderList)
        if (leastCost > curShopCost):
            leastCost = curShopCost
            chosenShop = shop
    return chosenShop

evaluation:编写好全部代码后,运行python3 autograder.py,使用一些case判断程序是否出错,并给出分数,输出信息:

Starting on 9-22 at 11:33:15

Question q1
===========

*** PASS: test_cases/q1/addition1.test
*** 	add(a,b) returns the sum of a and b
*** PASS: test_cases/q1/addition2.test
*** 	add(a,b) returns the sum of a and b
*** PASS: test_cases/q1/addition3.test
*** 	add(a,b) returns the sum of a and b

### Question q1: 1/1 ###


Question q2
===========

*** PASS: test_cases/q2/food_price1.test
*** 	buyLotsOfFruit correctly computes the cost of the order
*** PASS: test_cases/q2/food_price2.test
*** 	buyLotsOfFruit correctly computes the cost of the order
*** PASS: test_cases/q2/food_price3.test
*** 	buyLotsOfFruit correctly computes the cost of the order

### Question q2: 1/1 ###


Question q3
===========

Welcome to shop1 fruit shop
Welcome to shop2 fruit shop
*** PASS: test_cases/q3/select_shop1.test
*** 	shopSmart(order, shops) selects the cheapest shop
Welcome to shop1 fruit shop
Welcome to shop2 fruit shop
*** PASS: test_cases/q3/select_shop2.test
*** 	shopSmart(order, shops) selects the cheapest shop
Welcome to shop1 fruit shop
Welcome to shop2 fruit shop
Welcome to shop3 fruit shop
*** PASS: test_cases/q3/select_shop3.test
*** 	shopSmart(order, shops) selects the cheapest shop

### Question q3: 1/1 ###


Finished at 11:33:15

Provisional grades
==================
Question q1: 1/1
Question q2: 1/1
Question q3: 1/1
------------------
Total: 3/3