Question

In: Computer Science

You need to combine some small pieces of chocolate (1 ounce) and some large pieces of...

You need to combine some small pieces of chocolate (1 ounce) and some large pieces of chocolate (5 ounces) to make a goal. Assuming you always use as many large pieces as possible, complete the function "makeChocolate" which returns the number of small pieces that you will use. Return -1 if it is not possible. Be sure to carefully test your code before submitting it. Make sure to answer in python coding for full credit.
For example, makeChocolate(4,1,9) returns 4 because you use 1 large piece and 4 small pieces to get 9 ounces.
makeChocolate(4,1,10) returns -1 because 1 large and 4 small pieces are only 9 ounces, not enough to make 10 ounces.
makeChocolate(3,2,10) returns 0 because you would use 2 large pieces and 0 small pieces to make 10 ounces.

Solutions

Expert Solution

#function accepting small, large pieces and total ounces
def makeChocolate(small,large,ounce):
#infinite loop
while(1):
#if large itself is sufficient to make up the ounce
if(large*5==ounce):
#return 0
return 0
#else if large and small given make up ounce then
elif (large*5+small==ounce):
#return small
return small
#elif large itself is making ounces greater than ounce then
elif(large*5>ounce):
#decrement large and continue while loop
large=large-1
#elif large and small are making ounces greater than given ounce then
elif(large*5+small>ounce ):
#decrement small and continue while loop
small=small-1
#otherwise
else:
#return -1
return -1

#testing function with different test cases

print(makeChocolate(4,1,8))
print(makeChocolate(4,1,10))
print(makeChocolate(3,2,10))
print(makeChocolate(4,1,9))
print(makeChocolate(4,3,14))


Related Solutions

The Pat-A-Cake Pastry Shop makes chocolate cake in three sizes – Small, Medium, and Large. For...
The Pat-A-Cake Pastry Shop makes chocolate cake in three sizes – Small, Medium, and Large. For each size, the number of cakes made is an integer (i.e. the shop does not bake only half of a cake). The shop has the following amounts of the three main ingredients on hand – 400 ounces of cake flour, 550 ounces of caster sugar, and 150 ounces of cocoa powder. The table below provides details on the amount of each ingredient required for...
The Pat-A-Cake Pastry Shop makes chocolate cake in three sizes – Small, Medium, and Large. For...
The Pat-A-Cake Pastry Shop makes chocolate cake in three sizes – Small, Medium, and Large. For each size, the number of cakes made is an integer (i.e. the shop does not bake only half of a cake). The shop has the following amounts of the three main ingredients on hand – 400 ounces of cake flour, 550 ounces of caster sugar, and 150 ounces of cocoa powder. The table below provides details on the amount of each ingredient required for...
The average 1-ounce chocolate chip cookie contains 110 calories. A random sample of 15 different brands...
The average 1-ounce chocolate chip cookie contains 110 calories. A random sample of 15 different brands of 1-ounce cholocate chip cookies resulted in the following calorie amounts. At the  = 0.01 level, is there sufficient evidence that the average calorie content is greater than 110 calories? 100 125 140 125 120 125 105 115 130 100 135 115 115 120 110 State:__________________________________ CV: use table E, F, G, CV=___________________ TV: z, t, or 2  (circle one), TV=________________ Decide:________________________________...
Need some assistance, please! Suppose that you are a cow farmer whose cows produce large quantities...
Need some assistance, please! Suppose that you are a cow farmer whose cows produce large quantities of milk for suppliers. The government has decided to increase the minimum price of milk to $3.75 per gallon. Describe the pros and cons to this new price with respect to the producer and consumer. Be sure to include in your answer: price floors
Compare the functions of the small intestine and the large intestine. Why do proteins need to...
Compare the functions of the small intestine and the large intestine. Why do proteins need to incorporate into the triglyceride molecules to form the chylomicrons? Why can’t the chylomicrons move directly into the capillaries like monosaccharides and amino acids?
Demonstrating Referred Pain You’ll need a small plastic bin (approximately 1 quart volume or large enough...
Demonstrating Referred Pain You’ll need a small plastic bin (approximately 1 quart volume or large enough to place your elbow into it), water, and ice cubes to conduct this lab. Referred pain is a sensory experience in which pain is perceived as arising in one area of the body when in fact another, often quite remote area is receiving the painful stimulus. Thus the pain is said to be “referred” to a different area. Many of us have experienced referred...
You are given three small irregularly shaped pieces of metal and the following materials: Everything in...
You are given three small irregularly shaped pieces of metal and the following materials: Everything in your set of Common Lockers Your lab notebook Distilled water Cylinder of Copper Ruler 1 M HCl 1 M NaOH pH paper universal indicator balance If you wished to quantitatively determine the density of your metal pieces, which procedure would be best when provided with the resources listed above? * Determine the mass of each unknown metal. Then determine the volume of each piece...
Step 1: In separate small plastic bags or cups, combine various amounts of baking soda with...
Step 1: In separate small plastic bags or cups, combine various amounts of baking soda with a small fixed amount (such as 1/4 cup) of tepid water (noting its initial temperature with the thermometer if available), kneading or stirring the contents to mix, noting the temperature again and rinsing out each bag or cup after each trial. Record your observations in a tabular format. Does a temperature change accompany the dissolving process and is it affected by the amount of...
Give some example like the large organization has greater project failure than a small organization. You...
Give some example like the large organization has greater project failure than a small organization. You can provide some statistics on it (250-300 Words, No handwriting, No Copy Paste)
1) As a technician in a large pharmaceutical research firm, you need to produce 450. mL...
1) As a technician in a large pharmaceutical research firm, you need to produce 450. mL of a potassium dihydrogen phosphate buffer solution of pH = 7.04. The pKa of H2PO4− is 7.21. You have the following supplies: 2.00 L of 1.00 M KH2PO4 stock solution, 1.50 L of 1.00 M K2HPO4 stock solution, and a carboy of pure distilled H2O. How much 1.00 M KH2PO4 will you need to make this solution? (Assume additive volumes.) 2) If the normal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT