Open this lab document and keep it open during the lab -- it will be updated live!
def determineWinnings( weights:dict, prices:dict, capacity:int, items:list ):
""" Given a dictionary mapping item names -> weights,
another dictionary mapping item names -> prices,
a capacity for the bag,
and a list of items to put in the bag,
determine the winnings.
Assumes the weights and prices are ints.
If the total weight of the items is <= capacity,
then the winnings is the sum of the prices of the itmes.
Otherwise, return -1 to indicate that the bag broke!
"""
# use an accumulator pattern to determine the sum of the weights and prices
totalWeight:int = 0
totalPrice:int = 0
# walk over the items selected
for item in items:
# check that it's in the weights dictionary
if item in weights:
# if so, get its weight
itemWeight:int = weights[item]
# and add it to the total weight
totalWeight += itemWeight
else:
print( f"Oops! Didn't find the {item} in the weights...")
# check that it's in the prices dictionary
if item in prices:
# if so, get its price
itemPrice:int = prices[item]
# and add it to the total price
totalPrice += itemPrice
else:
print( f"Oops! Didn't find the {item} in the prices...")
# check if we went over capacity
if totalWeight > capacity:
return -1
else:
return totalPrice
def scenario1():
weights:dict = { "Gold ring":30,
"Silver ring":40,
"Platinum ring":50,
"Aluminum ring":60,
"Paper ring":20
}
prices:dict = { "Gold ring":500,
"Silver ring":600,
"Platinum ring":700,
"Aluminum ring":800,
"Paper ring":400
}
items:list = ["Gold ring", "Silver ring","Aluminum ring","Paper ring"]
winnings:int = determineWinnings(weights,prices,150,items)
print( f"The total winnings for {items} is {winnings}.")
def scenario2():
weights:dict = {"Lantern":10,
"Fountain":17,
"Locked Padlock":4,
"Golden Shoe":8,
"Goose Statue":14,
"Cheese Wheel":16,
"Potted Plant":5 }
prices:dict = { "Lantern":7,
"Fountain":34,
"Locked Padlock":0,
"Golden Shoe":29,
"Goose Statue":22,
"Cheese Wheel":37,
"Potted Plant":7 }
items:list = ["Cheese Wheel", "Golden Shoe", "Potted Plant"]
winnings:int = determineWinnings(weights,prices,150,items)
print( f"The total winnings for {items} is {winnings}.")
if __name__ == "__main__":
scenario1()
scenario2()