Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!