Created
July 30, 2013 05:55
-
-
Save gigamonkey/6110554 to your computer and use it in GitHub Desktop.
It seems to me the time complexity of this is non-linear in the amount as you can see in the way the number of iterations required grows. Unless maybe I've messed something up in my translation from Clojure to Python.
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/env python3 | |
def count_ways_to_make_change(coins, total): | |
iterations = 0 | |
ways = 0 | |
digit = 0 | |
counts = [0] * len(coins) | |
while digit < len(counts): | |
iterations += 1 | |
counts[digit] += 1 | |
new_sum = sum(d * c for d, c in zip(coins, counts)) | |
if new_sum < total: | |
digit = 0 | |
else: | |
if new_sum == total: ways += 1 | |
counts[digit] = 0 | |
digit += 1 | |
return ways, iterations | |
if __name__ == '__main__': | |
coins = [ 1, 5, 10, 25 ] | |
for amount in [ 100, 200, 400, 800 ]: | |
ways, iterations = count_ways_to_make_change(coins, amount) | |
print('amount: {}; ways: {}; iterations: {}'.format(amount, ways, iterations)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment