Question: After the following code is run, what are the values of n 1 and n 2 ? Note: we use some dictionary methods like dict.get

After the following code is run, what are the values of n1 and n2?
Note: we use some dictionary methods like dict.get() and dict.setdefault() in this code snippet because HuskyCT does not allow square brackets in question text. It is better in general to use square brackets for getting and setting items in a dictionary, unless you need the extra functionality provided by these methods.
def fewest_coins(amt, coins, solved=dict()):
"""Returns the minimum amount of coins needed to make amt"""
# Base case: we've solved this problem already
if amt in solved: return solved.get(amt)
# Base case: we can make this amount with 1 coin
elif amt in coins:
solved.setdefault(amt,1)
return solved.get(amt)
solved.setdefault(amt, float('inf'))
# Explore every coin from here, find minimum
for coin in coins:
if coin < amt:
n_branch =1+ fewest_coins(amt-coin, coins, solved)
if n_branch < solved.get(amt):
solved.pop(amt)
solved.setdefault(amt, n_branch)
return solved.get(amt)
L1= list((1,5,10,25))
L2= list((1,5,10,20,25))
n1= fewest_coins(40, L1)
n2= fewest_coins(40, L2)

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 Databases Questions!