You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
here is what this code does:
it takes all coins as sorted input list, so the maximum coin is at index 0. Then it checks if the amount is included in the first index value if not it will pop it out from the list. If it is included then that coin will be inserted to the result list.
def test_change(amount,coins,expected):
if expected == change(amount,coins):
return True
else:
return False
def change(amount, coins, result=list()):
if amount== 0:
return result
else:
if len(coins)>0:
maximum_coin = coins[0]
if amount>= maximum_coin:
result.append(maximum_coin)
amount= round(amount- maximum_coin, 2)
return change(amount,coins)
else:
if result:
coins.pop(0)
return change(amount,coins)
return result
print(test_change(6.44,[2,1,0.5,0.2,0.1,0.05,0.02,0.01],[2,2,2,0.2,0.2,0.02,0.02]))
Implement the informal algorithm introduced in Section "Greedy algorithms" for returning the minimum amount of coins for a change.
The text was updated successfully, but these errors were encountered: