Created
April 24, 2013 18:17
-
-
Save mlbright/5454271 to your computer and use it in GitHub Desktop.
greedy solution to the first problem at http://blog.jgc.org/2013/04/the-minimum-coin-problem-with-prize.html
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
#!/usr/bin/python | |
# Does this work? | |
# http://blog.jgc.org/2013/04/the-minimum-coin-problem-with-prize.html | |
coins = [200,50,50,100,100,20,20,20,10,5,5,2,2,1,1] | |
coins.sort() | |
coins.reverse() | |
paper = 170 | |
fortune = sum(coins) | |
print "my fortune is %d" % fortune | |
payback = fortune - paper | |
print "vendor can give back at most %d" % payback | |
change = [] | |
for coin in coins: | |
if (sum(change) + coin) <= payback: | |
change.append(coin) | |
print "change", change | |
print "price paid", sum(coins) - sum(change) | |
for coin in change: | |
coins.remove(coin) | |
print "coins deposited",coins | |
print "num deposited %s" % len(coins) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment