Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a function 'ingredients' that takes a recipe and returns a dictionary of the ingredients needed. Details: accepts one multiline str argument containing a recipe

Implement a function 'ingredients' that takes a recipe and returns a dictionary of the ingredients needed. Details:

  1. accepts one multiline str argument containing a recipe
  2. 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.
  3. instruction lines should be ignored
  4. 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)
  5. note that a particular ingredient may appear on more than one line, see sourdoughStarter below

The function ingredients should output a dictionary:

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

---------------------------------------------------------------------------------------------------------------

Sample usage:

>>> 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.'

>>> 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 flour: 113g water: 113g Cover loosely and let sit for 24 hours. Discard 1/2 of the starter and add flour: 113g water: 113g Mix and let sit for 24 hours. Discard 1/2 of the starter and add flour: 113g water: 113g Mix and let sit for 24 hours. Repeat a bunch of times ...'

>>> ingredients(sourdoughStarter) # flour and water listed multiple times

{'flour': 339, 'water': 339}

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions