Answer:
See explaination
Step-by-step explanation:
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)
def needed_ingredients(self, recipes):
""" Given a list of recipes, computes which ingredients to buy, and in which
quantity to be able to make all recipes. Can be implemented in 10 lines of code."""
# define the object new_pantry to contain the ingredients needed
new_pantry = Pantry({})
ingredients_present = self
# loop over the recipes in the list
for recipe in recipes:
# convert ingredients of recipe from AD to Pantry
ingredients = Pantry(recipe.ingredients)
# subtract the recipe's ingredients from ingredients_present
ingredients_present = ingredients_present - ingredients
# loop over the ingredients_present
for key in ingredients_present:
# if any ingredients's quantity < 0, add it to new_pantry
if ingredients_present[key] < 0:
# check if ingredient is present in new_pantry, add the quantity to existing quantity
if key in new_pantry:
new_pantry[key] += -1*(ingredients_present[key])
else: # if ingredient is not present in new_pantry, make a new entry for the ingredient
new_pantry[key] = -1*(ingredients_present[key])
ingredients_present[key] = 0 # set the quantity of the ingredient in ingredients_present to 0
return new_pantry