Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python Please PROVIDED CODE: PROBLEM: [] class AD (dict): definit__(self, *args, **kwargs) : This initializer simply passes all arguments to dict, so that

In Python Please

PROVIDED CODE:

image text in transcribed

image text in transcribed

image text in transcribed

PROBLEM:

image text in transcribed

[] class AD (dict): definit__(self, *args, **kwargs) : "" "This initializer simply passes all arguments to dict, so that we can create an AD with the same ease with which we can create a dict. There is no need, indeed, to repeat the initializer, but we leave it here in case we want to create attributes specific to an AD later."" super() . _init__(*args, **kwargs) def _add_(self, other): return AD._binary_op(self, other, lambda x, y: x + y, 0) def sub_(self, other): return AD. _binary_op (self, other, lambda x, y: x - y, 0) @staticmethod def binary op (left, right, op, neutral): r = AD() 1_keys = set (left.keys()) if isinstance(left, dict) else set(). r_keys = set (right.keys()) if isinstance(right, dict) else set(). for k in i_keys | r_keys: # If the right (or left) element is a dictionary (or an AD), # we get the elements from the dictionary; else we use the right # or left value itself. This implements a sort of dictionary # broadcasting. l_val = left.get(k, neutral) if isinstance(left, dict) else left r_val = right.get(k, neutral) if isinstance(right, dict) else right r[k] = op(l_val, r_val) return r For the first three problems, we will be writing methods to extend the Pantry class, defined below, that holds cooking ingredients. Notice that Pantry is a subclass of AD. Additionally, we will be using a Recipe class that specifies the ingredients that are required to make a dish. [] class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def __repr__(self): "" "To have a better representation of the recipe""" return f'(self.name}: {self.ingredients}' def hash__(self): ""Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id."" return hash(self.name) def_eq_(self, other): "" "For a recipe to equal another, their name and ingredients must match""" return self.name == other name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): """We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) U return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): "" "We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def repr_(self): "" "To have a better representation of the recipe""" return f'{self.name}: {self.ingredients}' def _hash_(self): "" "Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id." return hash(self.name) def _eg__(self, other): """For a recipe to equal another, their name and ingredients must match""" return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) To make sure everything is in order, we have some tests to ensure that our classes work. [] pantry = Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5,"lemon":6}) ramen = Recipe("Ramen", { "noodles":1, "soy sauce":2, "egg": 2, "bean sprouts":4}) assert_equal (pantry, Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5, "lemon":6})) assert_equal(ramen.name, "Ramen") assert_equal(ramen.ingredients, AD({ "noodles":1, "soy sauce":2, "egg":2, "bean sprouts":4})) - Problem 2: Checking recipe feasibility It would also be nice to know which recipes we can make given the current contents of our pantry. Your next task is to implement an individually_feasible method for the Pantry class, which takes a list of recipes and returns a list of the recipes that can be made individually using the ingredients in the pantry. For this problem, you do not need to worry about making multiple recipes at the same time. [] def individually_feasible(self, recipes): """Returns a list of recipes that could be individually made using the ingredients in the pantry. Can be implemented in 9 lines of code." # YOUR CODE HERE raise Not ImplementedError() Pantry. individually_feasible = individually_feasible [ ] # Tests for Pantry.individually_feasible pantry5 = Pantry({"chicken drumstick":5, "egg":10, "oil":6, "flour":8, "hot dog":3, "pepper":6}) fried_chicken = Recipe ("Fried Chicken", {"chicken drumstick": 1, "egg":2, "flour":1}) butter_fries = Recipe("Butter Fries", {"potato":4, "butter":1, "oil":1}) boiled_egg = Recipe ( "Boiled Egg", {"egg":1}) recipes = [fried_chicken, butter_fries, boiled_egg) # Order doesn't matter assert_equal(sorted (pantry5.individually_feasible (recipes), key=lambda x: x.name), [boiled_egg, fried_chicken]) # There may be repeats in the input list assert_equal (pantry5. individually_feasible([boiled_egg, boiled_egg]), [boiled_egg, boiled_egg]) # If it's not possible to make any of the recipes in the input list, # individually_feasible should return an empty list pantry6 = Pantry({ "cucumber":1, "tomato":1}) spinach_salad = Recipe ("Greek Salad", {"cucumber":1, "tomato":2, "feta cheese":1, "onion":0.3, "olive":10}) tomato sauce = Recipe ("Tomato Sauce", {"tomato":4, "garlic":3, "salt":1, "oil":1}) assert_equal(pantry6.individually_feasible([ spinach_salad, tomato sauce]), []) [] class AD (dict): definit__(self, *args, **kwargs) : "" "This initializer simply passes all arguments to dict, so that we can create an AD with the same ease with which we can create a dict. There is no need, indeed, to repeat the initializer, but we leave it here in case we want to create attributes specific to an AD later."" super() . _init__(*args, **kwargs) def _add_(self, other): return AD._binary_op(self, other, lambda x, y: x + y, 0) def sub_(self, other): return AD. _binary_op (self, other, lambda x, y: x - y, 0) @staticmethod def binary op (left, right, op, neutral): r = AD() 1_keys = set (left.keys()) if isinstance(left, dict) else set(). r_keys = set (right.keys()) if isinstance(right, dict) else set(). for k in i_keys | r_keys: # If the right (or left) element is a dictionary (or an AD), # we get the elements from the dictionary; else we use the right # or left value itself. This implements a sort of dictionary # broadcasting. l_val = left.get(k, neutral) if isinstance(left, dict) else left r_val = right.get(k, neutral) if isinstance(right, dict) else right r[k] = op(l_val, r_val) return r For the first three problems, we will be writing methods to extend the Pantry class, defined below, that holds cooking ingredients. Notice that Pantry is a subclass of AD. Additionally, we will be using a Recipe class that specifies the ingredients that are required to make a dish. [] class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def __repr__(self): "" "To have a better representation of the recipe""" return f'(self.name}: {self.ingredients}' def hash__(self): ""Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id."" return hash(self.name) def_eq_(self, other): "" "For a recipe to equal another, their name and ingredients must match""" return self.name == other name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): """We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) U return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): "" "We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def repr_(self): "" "To have a better representation of the recipe""" return f'{self.name}: {self.ingredients}' def _hash_(self): "" "Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id." return hash(self.name) def _eg__(self, other): """For a recipe to equal another, their name and ingredients must match""" return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) To make sure everything is in order, we have some tests to ensure that our classes work. [] pantry = Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5,"lemon":6}) ramen = Recipe("Ramen", { "noodles":1, "soy sauce":2, "egg": 2, "bean sprouts":4}) assert_equal (pantry, Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5, "lemon":6})) assert_equal(ramen.name, "Ramen") assert_equal(ramen.ingredients, AD({ "noodles":1, "soy sauce":2, "egg":2, "bean sprouts":4})) - Problem 2: Checking recipe feasibility It would also be nice to know which recipes we can make given the current contents of our pantry. Your next task is to implement an individually_feasible method for the Pantry class, which takes a list of recipes and returns a list of the recipes that can be made individually using the ingredients in the pantry. For this problem, you do not need to worry about making multiple recipes at the same time. [] def individually_feasible(self, recipes): """Returns a list of recipes that could be individually made using the ingredients in the pantry. Can be implemented in 9 lines of code." # YOUR CODE HERE raise Not ImplementedError() Pantry. individually_feasible = individually_feasible [ ] # Tests for Pantry.individually_feasible pantry5 = Pantry({"chicken drumstick":5, "egg":10, "oil":6, "flour":8, "hot dog":3, "pepper":6}) fried_chicken = Recipe ("Fried Chicken", {"chicken drumstick": 1, "egg":2, "flour":1}) butter_fries = Recipe("Butter Fries", {"potato":4, "butter":1, "oil":1}) boiled_egg = Recipe ( "Boiled Egg", {"egg":1}) recipes = [fried_chicken, butter_fries, boiled_egg) # Order doesn't matter assert_equal(sorted (pantry5.individually_feasible (recipes), key=lambda x: x.name), [boiled_egg, fried_chicken]) # There may be repeats in the input list assert_equal (pantry5. individually_feasible([boiled_egg, boiled_egg]), [boiled_egg, boiled_egg]) # If it's not possible to make any of the recipes in the input list, # individually_feasible should return an empty list pantry6 = Pantry({ "cucumber":1, "tomato":1}) spinach_salad = Recipe ("Greek Salad", {"cucumber":1, "tomato":2, "feta cheese":1, "onion":0.3, "olive":10}) tomato sauce = Recipe ("Tomato Sauce", {"tomato":4, "garlic":3, "salt":1, "oil":1}) assert_equal(pantry6.individually_feasible([ spinach_salad, tomato sauce]), [])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions