In: Computer Science
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.
#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))