-
-
Save mikezink/1dea82c8cb781884523723692e2f5861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dpMakeChange(coinValueList,change,minCoins,coinsUsed): | |
for cents in range(change+1): | |
coinCount = cents | |
newCoin = 1 | |
for j in [c for c in coinValueList if c <= cents]: | |
if minCoins[cents-j] + 1 < coinCount: | |
coinCount = minCoins[cents-j]+1 | |
newCoin = j | |
minCoins[cents] = coinCount | |
coinsUsed[cents] = newCoin | |
return minCoins[change] | |
def printCoins(coinsUsed,change): | |
coin = change | |
while coin > 0: | |
thisCoin = coinsUsed[coin] | |
print(thisCoin) | |
coin = coin - thisCoin | |
def main(): | |
amnt = 63 | |
clist = [1,5,10,21,25] | |
coinsUsed = [0]*(amnt+1) | |
coinCount = [0]*(amnt+1) | |
print("Making change for",amnt,"requires") | |
print(dpMakeChange(clist,amnt,coinCount,coinsUsed),"coins") | |
print("They are:") | |
printCoins(coinsUsed,amnt) | |
print("The used list is as follows:") | |
print(coinsUsed) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment