From 745782602323f26f586588c3e8ca9522c82fc0da Mon Sep 17 00:00:00 2001 From: Tue Herlau <tuhe@dtu.dk> Date: Fri, 17 Jan 2025 15:45:26 +0100 Subject: [PATCH] Fruit homework fixes --- irlc/ex00/fruit_homework.py | 62 ++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/irlc/ex00/fruit_homework.py b/irlc/ex00/fruit_homework.py index 89cb369..e67c3f6 100644 --- a/irlc/ex00/fruit_homework.py +++ b/irlc/ex00/fruit_homework.py @@ -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. """ # TODO: 1 lines missing. raise NotImplementedError("Implement function body") -# -# def fruits_ordered(order_dict): + + +class BasicFruitShop: """This is a simple class that represents a fruit-shop. 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 = {'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 to get the price. You don't need to do exception handling here. """ -# """ -# order_dict = {'apple': 5, 'pear': 2, ...} where the numbers are the quantity ordered. -# -# Hints: Dictionary comprehension like: -# > for fruit, pounds in order_dict.items() -# > self.getCostPerPound(fruit) allows you to get cost of a fruit -# > 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]. -# """ + # TODO: 1 lines missing. + raise NotImplementedError("Return cost of fruit as a floating point number") + + +if __name__ == '__main__': + "This code runs when you invoke the script from the command line (but not otherwise)" + """ 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 > 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 C: Class inheritance """ - # """ Part C: Using classes """ -# TODO: 60 lines missing. -raise NotImplementedError("Implement function body") - # print("For the order", order, " the best shop is", shop_smart(order, shops).name) + price1 = {"apple": 4, "pear": 8, 'orange': 10} + shop1 = BasicFruitShop("Alis Funky Fruits", price1) + + price2 = {'banana': 9, "apple": 5, "pear": 7, 'orange': 11} + 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)) -- GitLab