Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part B Here is solution code for the Recipe class from last time. To simplify the code, we have removed the input checking in the

Part B
Here is solution code for the Recipe class from last time. To simplify the code, we have removed the input checking in the _init _ method, as well
as the _ method.
In []: class Recipe:
def (self, title, ingredients, directions):
self.title = title
self.ingredients = ingredients
self.directions = directions
def_rmul_(self, multiplier):
multiplied_ingredients ={key : multiplier*val for key, val in self.ingredients.items()}
return Recipe(self.title, multiplied_ingredients, self.directions)
Now, implement subtraction in which the first argument is a Pantry and the second argument is a Recipe . The relevant magic method for this is
called _(), and should be implemented in the Pantry class. Here's how subtraction my_pantry - my_recipe should work:
You may assume that my_pantry and my_recipe are valid instances of their class. In particular, all quantities of ingredients are positive numbers
( int s or float s).
If all keys from my_recipe. ingredients are present in my_pantry, and if they all have values smaller than their values in my_pantry, then the
result of my_pantry - my_recipe is a new Pantry object in which the values corresponding to the keys have been reduced by the quantity in
my_recipe.
If a key is present in my_pantry but not in my_recipe, then it is treated as though it is present in my_pantry with value 0.
For now, you can assume that the conditions of clause 2. are met, and that subtraction should therefore "work." That is, you can assume that you have
enough of all ingredients in the pantry to make the recipe. For example, with my_pantry from Part A,
title = "cookies"
ingredients
"flour (grams)": 400,
"butter (grams)" : 200,
"salt (arams)" : 10.
image text in transcribed

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

More Books

Students also viewed these Databases questions

Question

What is operatiing system?

Answered: 1 week ago

Question

8. Demonstrate aspects of assessing group performance

Answered: 1 week ago