Skip to content
Snippets Groups Projects
Commit 74578260 authored by tuhe's avatar tuhe
Browse files

Fruit homework fixes

parent f889866a
No related branches found
No related tags found
No related merge requests found
...@@ -18,41 +18,53 @@ def mean_value(p_dict : dict) -> float: ...@@ -18,41 +18,53 @@ def mean_value(p_dict : dict) -> float:
Hint: Look at the .items() method and the build-in sum(my_list) method. """ Hint: Look at the .items() method and the build-in sum(my_list) method. """
# TODO: 1 lines missing. # TODO: 1 lines missing.
raise NotImplementedError("Implement function body") raise NotImplementedError("Implement function body")
#
# def fruits_ordered(order_dict):
class BasicFruitShop:
"""This is a simple class that represents a fruit-shop. """This is a simple class that represents a fruit-shop.
You should give it a dictionary of prices when you initialize it.""" You should give it a dictionary of prices when you initialize it."""
def __init__(self, name : str, prices : dict):
"""prices is a dictionary of the form {fruit_name: cost}. For instance """prices is a dictionary of the form {fruit_name: cost}. For instance
prices = {'apple': 5, 'orange': 6} """ prices = {'apple': 5, 'orange': 6} """
self.name = name
self.prices = prices
def cost(self, fruit : str) -> float:
""" Return the cost in pounds of the fruit with name 'fruit'. It uses the self.prices variable """ Return the cost in pounds of the fruit with name 'fruit'. It uses the self.prices variable
to get the price. to get the price.
You don't need to do exception handling here. """ You don't need to do exception handling here. """
# """ # TODO: 1 lines missing.
# order_dict = {'apple': 5, 'pear': 2, ...} where the numbers are the quantity ordered. raise NotImplementedError("Return cost of fruit as a floating point number")
#
# Hints: Dictionary comprehension like:
# > for fruit, pounds in order_dict.items() if __name__ == '__main__':
# > self.getCostPerPound(fruit) allows you to get cost of a fruit "This code runs when you invoke the script from the command line (but not otherwise)"
# > the total is sum of {pounds} * {cost_per_pound}
# """
# """
# order_dict: dictionary {'apple': 3, ...} of fruits and the pounds ordered
# fruitShops: List of OnlineFruitShops
#
# Hints:
# > Remember there is a s.price_of_order method
# > Use this method to first make a list containing the cost of the order at each fruit shop
# > List has form [cost1, cost2], then find the index of the smallest value (the list has an index-function)
# > return fruitShops[lowest_index].
# """
""" Quesion 1: Lists and basic data types """ """ Quesion 1: Lists and basic data types """
print("add(2,5) function should return 7, and it returned", add(2, 5))
animals = ["cat", "giraffe", "wolf"]
print("The nice animals are", misterfy(animals))
""" """
This problem represents the probabilities of a loaded die as a dictionary such that This problem represents the probabilities of a loaded die as a dictionary such that
> p(roll=3) = p_dict[3] = 0.15. > p(roll=3) = p_dict[3] = 0.15.
""" """
p_die = {1: 0.20,
2: 0.10,
3: 0.15,
4: 0.05,
5: 0.10,
6: 0.40}
print("Mean roll of die, sum_{i=1}^6 i * p(i) =", mean_value(p_die))
""" Part B: A simple class """ """ Part B: A simple class """
# """ Part C: Class inheritance """ price1 = {"apple": 4, "pear": 8, 'orange': 10}
# """ Part C: Using classes """ shop1 = BasicFruitShop("Alis Funky Fruits", price1)
# TODO: 60 lines missing.
raise NotImplementedError("Implement function body") price2 = {'banana': 9, "apple": 5, "pear": 7, 'orange': 11}
# print("For the order", order, " the best shop is", shop_smart(order, shops).name) shop2 = BasicFruitShop("Hansen Fruit Emporium", price2)
fruit = "apple"
print("The cost of", fruit, "in", shop1.name, "is", shop1.cost(fruit))
print("The cost of", fruit, "in", shop2.name, "is", shop2.cost(fruit))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment