Answered step by step
Verified Expert Solution
Question
1 Approved Answer
def give_change (amount, coins): Given the amount of money, expressed as the total number of kopecks of Latveria, Ruritania, Mon- tenegro or some other
def give_change (amount, coins): Given the amount of money, expressed as the total number of kopecks of Latveria, Ruritania, Mon- tenegro or some other one of those such vaguely Slavic fictional countries that Tintin and similar hearty fellows like to visit for a wacky chase after the current year's MacGuffin, followed by the list of available denominations of coins also expressed as kopecks, this function should create and re- turn a list of coins that add up to the amount using the greedy approach where you use as many of the highest denomination coins when possible before moving on to the next lower denomination. The list of coin denominations is guaranteed to be given in descending sorted order, as should your returned result also be. amount 64 123 100 coins [50, 25, 10, 5, 1] [100, 25, 10, 5, 1] [42, 17, 11, 6, 1] Expected result [50, 10, 1, 1, 1, 1] [100, 10, 10, 1, 1, 1] [42, 42, 11, 1, 1, 1, 1, 1] Along with its countless variations, this problem is a computer science classic when modified to minimize the total number of returned coins. The above greedy approach then no longer produces the optimal result for all possible coin denominations. For example, using the simple coin denomi- nations of [50, 30, 1] and the amount of sixty kopecks to be exchanged, the greedy solution [50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ends up using eleven coins due to the unfortunate first choice that prevents it from using any of the 30-kopeck coins. Those 30-kopeck coins sure would come handy here, seeing that the optimal solution [30, 30] needs only two such coins! A more advanced recursive algorithm examines both sides of the "take it or leave it" decision for each coin and chooses the choice that ultimately leads to a superior outcome. The intermediate results of this recursion should then be memoized to avoid blowing up the running time exponentially.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started