Question: Python Problem: Given Code: Problem: (if you check previous problem: https://www.chegg.com/homework-help/questions-and-answerseed-help-python-problem-given-codes-problem-check-first-problem-https-wwwcheggcom-homewor-q44265448) [] class AD (dict): def __init_(self, *args, **kwargs): This initializer simply passes all

Python Problem:

Given Code:

Python Problem: Given Code: Problem: (if you check previous problem: https://www.chegg.com/homework-help/questions-and-answerseed-help-python-problem-given-codes-problem-check-first-problem-https-wwwcheggcom-homewor-q44265448) []class AD (dict): def __init_(self, *args, **kwargs): "" "This initializer simply passes

Problem: (if you check previous problem: https://www.chegg.com/homework-help/questions-and-answerseed-help-python-problem-given-codes-problem-check-first-problem-https-wwwcheggcom-homewor-q44265448)

all arguments to dict, so that we can create an AD withthe same ease with which we can create a dict. There is

[] class AD (dict): def __init_(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 1_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. 1_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 [] 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 _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) 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 3: Checking simultaneous recipe feasibility Similar to the previous problem, we want to know recipes we can make given the current contents of our pantry, but this time, for multiple recipes at once. For this problem, you will implement a simul_feasible_nr method for the Pantry class. This method takes a list of recipes and returns a list containing all of the different combinations of recipes that could be simultaneously made using the available ingredients in the pantry, without repeating any recipe in each combination. (The "nr" in the name stands for "non-repeating".) simul_feasible_nr should return a list of lists. That is, the returned value should be a list, where each element in that list is itself a list representing a particular combination of recipes. Neither the order of the outer list nor the order of the inner list matters. A combination of recipes has a minimum of 1 recipe and has no maximum size, as long as the pantry has enough ingredients. Hints: simul_feasible_nr may call the individually_feasible method defined above. You may find it useful to define an inner function that calls itself recursively. [] def simul_feasible_nr(self, recipes): "*"Returns a list containing different combinations of recipes that could be made simultaneously using the ingredients available in the pantry."" # YOUR CODE HERE raise NotImplementedError() Pantry.simul_feasible_nr = simul_feasible_nr If simul_feasible_nr is working correctly, then the below cell should print something like this: Recipe(s) that can be made simultaneously: * Fried Rice Recipe(s) that can be made simultaneously: * Fried Rice * Spinach-Mushroom Scrambled Eggs Recipe(s) that can be made simultaneously: * Rice and Beans Recipe(s) that can be made simultaneously: * Rice and Beans * Spinach-Mushroom Scrambled Eggs Recipe(s) that can be made simultaneously: * Spinach-Mushroom Scrambled Eggs [] p1 = Pantry({"egg":6, "beans":11, "rice":7, "onion": 1, "mushrooms":1, "spinach":1, "cheese":1, "soy sauce":1, "butter":1, "oil":2}) fried_rice = Recipe ("Fried Rice", {"rice":4, "egg":2, "onion":1,"soy sauce":1, "oil":1}) rice_and_beans = Recipe ("Rice and Beans", {"rice":4, "beans":4, "oil":1}) spinach_mushroom_scrambled_eggs = Recipe("Spinach-Mushroom Scrambled Eggs", {"egg":4, "mushrooms":1, "spinach":0.2, "cheese":1, "butter":1}) watermelon = Recipe("Watermelon", {"watermelon":1}) p1_simul_feasible_nr = pi. simul_feasible_nr([fried_rice, rice_and_beans, spinach_mushroom_scrambled_eggs, watermelon)) for collection in pl_simul_feasible_nr: print("Recipe(s) that can be made simultaneously:") for recipe in collection: print(" * {}".format(recipe.name)) print("") [] # Tests for Pantry.simul_feasible_nr p2 = Pantry({"a":3,"6":3, "c":3}) recipe1 = Recipe ("recipel", {"a":1, "b":1, "c":1}) recipe2 = Recipe ("recipe2", {"a":2, "b":2, "c":2) recipe3 = Recipe("recipe3", {"a": 3, "b":3, "c":3}) p2_simul_feasible_nr = p2.simul_feasible_nr([recipei, recipe2, recipe 3]) p2_result = set() for collection in p2_simul_feasible_nr: P2_result.add(frozenset(collection)) # recipel and recipe2 can be made simultaneously assert_equal (p2_result, set([frozenset([recipe1]), frozenset([recipe2]), frozenset([recipe3]), frozenset([recipei, recipe2])))) [] class AD (dict): def __init_(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 1_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. 1_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 [] 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 _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) 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 3: Checking simultaneous recipe feasibility Similar to the previous problem, we want to know recipes we can make given the current contents of our pantry, but this time, for multiple recipes at once. For this problem, you will implement a simul_feasible_nr method for the Pantry class. This method takes a list of recipes and returns a list containing all of the different combinations of recipes that could be simultaneously made using the available ingredients in the pantry, without repeating any recipe in each combination. (The "nr" in the name stands for "non-repeating".) simul_feasible_nr should return a list of lists. That is, the returned value should be a list, where each element in that list is itself a list representing a particular combination of recipes. Neither the order of the outer list nor the order of the inner list matters. A combination of recipes has a minimum of 1 recipe and has no maximum size, as long as the pantry has enough ingredients. Hints: simul_feasible_nr may call the individually_feasible method defined above. You may find it useful to define an inner function that calls itself recursively. [] def simul_feasible_nr(self, recipes): "*"Returns a list containing different combinations of recipes that could be made simultaneously using the ingredients available in the pantry."" # YOUR CODE HERE raise NotImplementedError() Pantry.simul_feasible_nr = simul_feasible_nr If simul_feasible_nr is working correctly, then the below cell should print something like this: Recipe(s) that can be made simultaneously: * Fried Rice Recipe(s) that can be made simultaneously: * Fried Rice * Spinach-Mushroom Scrambled Eggs Recipe(s) that can be made simultaneously: * Rice and Beans Recipe(s) that can be made simultaneously: * Rice and Beans * Spinach-Mushroom Scrambled Eggs Recipe(s) that can be made simultaneously: * Spinach-Mushroom Scrambled Eggs [] p1 = Pantry({"egg":6, "beans":11, "rice":7, "onion": 1, "mushrooms":1, "spinach":1, "cheese":1, "soy sauce":1, "butter":1, "oil":2}) fried_rice = Recipe ("Fried Rice", {"rice":4, "egg":2, "onion":1,"soy sauce":1, "oil":1}) rice_and_beans = Recipe ("Rice and Beans", {"rice":4, "beans":4, "oil":1}) spinach_mushroom_scrambled_eggs = Recipe("Spinach-Mushroom Scrambled Eggs", {"egg":4, "mushrooms":1, "spinach":0.2, "cheese":1, "butter":1}) watermelon = Recipe("Watermelon", {"watermelon":1}) p1_simul_feasible_nr = pi. simul_feasible_nr([fried_rice, rice_and_beans, spinach_mushroom_scrambled_eggs, watermelon)) for collection in pl_simul_feasible_nr: print("Recipe(s) that can be made simultaneously:") for recipe in collection: print(" * {}".format(recipe.name)) print("") [] # Tests for Pantry.simul_feasible_nr p2 = Pantry({"a":3,"6":3, "c":3}) recipe1 = Recipe ("recipel", {"a":1, "b":1, "c":1}) recipe2 = Recipe ("recipe2", {"a":2, "b":2, "c":2) recipe3 = Recipe("recipe3", {"a": 3, "b":3, "c":3}) p2_simul_feasible_nr = p2.simul_feasible_nr([recipei, recipe2, recipe 3]) p2_result = set() for collection in p2_simul_feasible_nr: P2_result.add(frozenset(collection)) # recipel and recipe2 can be made simultaneously assert_equal (p2_result, set([frozenset([recipe1]), frozenset([recipe2]), frozenset([recipe3]), frozenset([recipei, recipe2]))))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!