kony345p
09.05.2020 •
Computers and Technology
Needed ingredients for recipes Before making a dish, we want to check whether we already have the necessary ingredients in our pantry or whether we need to go shopping. For this problem, you will implement a method needed_ingredients for the Pantry class. needed_ingredients takes a list of recipes as its argument and returns a new Pantry containing everything missing from our pantry to make all the recipes in the list. If our pantry already has all the necessary ingredients in the needed quantities, needed_ingredients returns an empty Pantry. There may be repeated recipes in the input list, and the input list may be empty. # You may wish to use defaultdict to implement needed_ingredients. from collections import defaultdict 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."** #Code here raise Not ImplementedError()
Solved
Show answers
More tips
- F Family and Home Tender Care for Your Parquet: Is it Possible to Clean Parquet?...
- S Society and Politics Is It Fact or Fiction? Let s Talk About Anton Chekhov s Pseudonym...
- S Sport Playing Bowling: Rules and Advice for Novices...
- C Computers and Internet How to Properly Repartition a Hard Drive?...
- A Auto and Moto What Is the Cost of Customs Clearance for a Car in Russia?...
- L Leisure and Entertainment Should You Buy a Ceramic Knife?...
- C Computers and Internet How to easily and quickly disable Firebug in Gmail and Google Docs...
- G Goods and services How to sew a ribbon: Tips for beginners...
- F Food and Cooking How to Make Mayonnaise at Home? Secrets of Homemade Mayonnaise...
- C Computers and Internet Which Phone is Best for Internet Surfing?...
Answers on questions: Computers and Technology
- M Mathematics Graph ATUV with vertices T(- 3, 2) , U(- 1, 0) , and V(- 4, - 2) and its image after the glide reflection. Translation : (x,y) (x+5,y) Reflection : in the X-axis...
- M Mathematics Which of the following would not work as a common denominator of 7/9 and 16/15? a. 45 b. 60 c. 90 d. 135...
- S Social Studies NEED HELP ASAP, WILL GIVE BRAINLIEST 1. The right to privacy is never directly mentioned in the Constitution. True or False2. Civil Rights are the same as Civil Liberties....
- E English 5. Screeched, yelled, and murmured are all examples of what? dialogue tags dialogue markers dialogue titles dialogue labels...
- H History Why was the bank war important? Also what was the election of 1828 and why that important?...
Ответ:
See explaination
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
Ответ:
Answer is in the file below
cutt[dot]ly/lxkFVRg