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 n and n
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 fewestcoinsamt coins:
return fewestcoinsamt coins, dict
def fewestcoinsamt coins, solved:
Returns the minimum amount of coins needed to make amt"""
# Base case: we've solved this problem already
if amt in solved: return solved.getamt
# Base case: we can make this amount with coin
elif amt in coins:
solved.setdefaultamt
return solved.getamt
solved.setdefaultamt floatinf
# Explore every coin from here, find minimum
for coin in coins:
if coin amt:
nbranch fewestcoinsamtcoin, coins, solved
if nbranch solved.getamt:
solved.popamt
solved.setdefaultamt nbranch
return solved.getamt
L list
L list
n fewestcoins L
n fewestcoins L
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
