Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

Students also viewed these Databases questions