Question

In: Computer Science

Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...

Use Python 3.8:

Problem Description

Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups.

This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself.

Here is an example recipe that comes from the story "Chitty Chitty Bang Bang", written by Ian Fleming, who is much better known for introducing the world to James Bond:

This is a recipe scaler for serving large crowds!

Enter one ingredient per line, with a numeric value first.
Indicate the end of input with an empty line.
4 tbsp cocoa
1/4 pound butter
2 tsp corn syrup
1 can evaporated milk
1 tsp water

Here is the recipe that has been recorded
 4    tbsp     cocoa
 1/4  pound    butter
 2    tsp      corn syrup
 1    can      evaporated milk
 1    tsp      water
How many does this recipe serve? 16
How many people must be served? 25
Multiplying the recipe by 2

 8    tbsp     cocoa
 2/4  pound    butter
 4    tsp      corn syrup
 2    can      evaporated milk
 2    tsp      water

Serves 32

NOTE: The recipe rounds upwards, since it is usually not practical to obtain fractional cans or fractional eggs, etc.

Your program must obtain a complete recipe (not necessarily this one), echo it with good formatting, and then scale it up as shown above.

Program Hints:

Attractive user-friendly output is rather straightforward, with the help of Python's string formatting features. User-friendly input is a little trickier, but the split function from Unit 2 can be very helpful:

First hint:

The name of an ingredient might be more than one word. This will place all of the extra words into a single string variable 'item':

quant, unit, item = line.split(' ',2)      # pull off at most 2 words from the front

Second hint:

Sometimes the measure will be fractional. We can recognize that if the number contains a slash.

if '/' in quant: 
    numer, denom = quant.split('/')        # get the parts of the fraction

The rest is left up to the student -- since this is a string operation and this fraction represents a number.

Second Part:

No doubt the output seems to be a little strange to ask for 2/4 pounds of butter. One might think it would be better to ask for 1/2.

Modify the program so that all fractions are reduced to their lowest terms. There is a function in the Python math module named gcd that can help with this. (You can email course personnel if you need help accessing the math features.)

Also, express all improper fractions (where the numerator exceeds the denominator) as mixed fractions. Scaling this recipe by a factor of 10 would ask for 2 1/2 pounds of butter.

And of course, the resulting output should still be easy to read.

Other Guidelines:

Clarity of code is still important here -- and clear code is less likely to have bugs.

In particular, there should be very good and clear decisions in the code.

And there will be a penalty for usage of break or continue statements.

Planning out the design of the solution before diving into code will help!

The simplest solutions would use a list, but without any indexing on that list
(or use of range() to get those indexes).  Let Python help you fill and traverse the recipe.

Storing the entire recipe in a single list before splitting things up often produces much simpler programs than trying to store everything into multiple separate lists!

IMPORTANT NOTE: As above, the recipe is provided as input to the program -- it is not part of the program itself. The program may not assume it knows what the ingredients are, or how many there are, or which ingredients have fractions and which ones do not. It must work for any number of valid input lines.

TASKS:

Recipe Data Structure: Effectively uses list (either parallel lists or lists of structures)

Input Recipe: Clearly reads input until blank line encountered

Serving Inputs: Correctly inputs two values: how many recipe serves, and how many will be served

Computing the Scale: math.ciel; if/else to round up; or anything else equivalent

Parsing the ingredients: Correctly parses ingredients (using given 'tricks') May be done at any point in the program

Scaling the recipe: Multiplies whole numbers and numerators by chosen scaling factor

Reduced fraction: Reduces using gcd; denominator 1 reported as whole numbers

Output presentation: Uses string formatting to present output recipe

Compilation: Program runs fully without change (with valid inputs of 3+ words separated with single spaces)

Correctness: Program behaves as expected (accounting for known errors above)

Program can handle "1 egg" as an ingredient (with only once space)

Only Python 3.8 will be allowed.

Solutions

Expert Solution

Solution:

Givendata:

Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups.

This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself.

Answer:

print("Enter one ingredient per line,with numeric valie first.")
print("Indicate the end of input with an empty line.")
quant=[]
units=[]
items=[]#Declaring 3 lists for 3 parts of ingrediants
while(True):
ing=input("");#Reading the ingrediants
if(ing):
#Here we split the ingrediant
quantity,uni,it=ing.split(" ",2)
quant.append(quantity)
units.append(uni)
items.append(it)
else:#If new line we break the loop
break
print("Here is the recipe that has been recorded")
for i in range(len(quant)):#printing the recipy
print(quant[i]+"\t"+units[i]+"\t"+items[i])
people=int(input("How many does the recipe serve?"))
#No of people the ingreadiants serve
required=int(input("How many people must be served?"))
#Required to serve
mul=1
while(mul*people<required):
#this loop iterates untill the mul*peopele>=required
mul+=1
print("Multiply the recipy by",mul)
#printing the multiple of the reciple
for i in range(len(quant)):
#in this loop we print the multiple of the each ingrediant
if(len(quant[i])==1):
print(str(mul*int(quant[i]))+"\t"+units[i]+"\t"+items[i])
else:
x=quant[i].split("/")
numer=int(x[0])*mul
denom=int(x[1])
print(str(numer)+"/"+str(denom)+"\t"+units[i]+"\t"+items[i])
print("\nServes",mul*people)
#Here we print how many people it can serve

Output:

Indentation:

PLEASE GIVEME THUMBUP...........


Related Solutions

Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
Many people tend avoid the use sucralose when possible. Why might it be concerning that an...
Many people tend avoid the use sucralose when possible. Why might it be concerning that an artificial sugar look so much like the natural sugar? (Consider: hydrolysis is just simply the reverse reaction that is used to store glucose within glycogen)
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Problem Description An organization has a number of buildings and employees. Every employee has an office...
Problem Description An organization has a number of buildings and employees. Every employee has an office in one of the buildings. The application will store the employees, the buildings, and the relationships between employees and buildings. Entity interface The Entity interface represents an employee or a building. Every object of type Entity has a single method, with the following characteristics. • Name: getName() • Returns the name of the entity (a String object) • No parameters Employee class This class...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive. In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4]. Using the function intToList, implement the function digitsCount(a, b) that returns a list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT