Implement a function ingredients in Python that takes a recipe and returns a dictionary of the ingredients needed. Details:   accepts one multiline str argument containing a recipe each line of the recipe is either an instruction line or an ingredient line. The ingredient lines contain the character ':' , the instruction lines do not. instruction lines should be ignored ingredient lines will be structured like 'active dry yeast: 10g' . This breaks down as: an ingredient, e.g., 'active dry yeast' (can be multiple words) the character ':' followed by a space the quantity, e.g., '10g' , which will always be an integer (number of digits can vary) followed always by the letter 'g' ( for grams) note- that a particular ingredient may appear on more than one line, see sourdoughStarter below The function ingredients should output a dictionary: keys are ingredients each value is an int specifying the number grams of that ingredient used in the recipe if an ingredient appears on more than one line, the value should be the total amount of the ingredient required

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Implement a function ingredients in Python that takes a recipe and returns a dictionary of the ingredients needed.

Details:

 

  • accepts one multiline str argument containing a recipe
  • each line of the recipe is either an instruction line or an ingredient line. The ingredient lines contain the character ':' , the instruction lines do not.
  • instruction lines should be ignored
  • ingredient lines will be structured like 'active dry yeast: 10g' . This breaks down as:

an ingredient, e.g., 'active dry yeast' (can be multiple words)

the character ':' followed by a space

the quantity, e.g., '10g' , which will always be an integer (number of digits can vary) followed always by the letter 'g' ( for grams)

  • note- that a particular ingredient may appear on more than one line, see sourdoughStarter below

The function ingredients should output a dictionary:

  • keys are ingredients
  • each value is an int specifying the number grams of that ingredient used in the recipe
  • if an ingredient appears on more than one line, the value should be the total amount of the ingredient required

 

Below is an example of a output from the code:

 

>>> ganache='semi-sweet chocolate: 224g\nheavy cream: 224g\nChop chocolate,
place in a bowl. Heat cream until it simmers.\nAdd cream to bowl, let stand for 3
minutes, then whisk until smooth.'
>>> print(ganache)
semi-sweet chocolate: 224g
heavy cream: 224g
Chop chocolate, place in a bowl. Heat cream until it simmers.
Add cream to bowl, let stand for 3 minutes, then whisk until smooth.
>>> ingredients(ganache)
{'semi-sweet chocolate': 224, 'heavy cream': 224}
>>> ingredients(ganache)['heavy cream']
224
>>> sourdoughStarter= 'Mix\nflour: 113g\nwater: 113g\nCover loosely and let sit
for 24 hours.\nDiscard 1/2 of the starter and add\nflour: 113g\nwater: 113g\nMix
and let sit for 24 hours. \nDiscard 1/2 of the starter and add\nflour:
113g\nwater: 113g\nMix and let sit for 24 hours. \nRepeat a bunch of times ...'
>>> ingredients(sourdoughStarter) # flour and water listed multiple times
{'flour': 339, 'water': 339}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY