Skip to content

Instantly share code, notes, and snippets.

@citadelgrad
Last active September 25, 2019 16:53
Show Gist options
  • Save citadelgrad/5af44788527c14995f6d393dbf3c7bfd to your computer and use it in GitHub Desktop.
Save citadelgrad/5af44788527c14995f6d393dbf3c7bfd to your computer and use it in GitHub Desktop.
def get_coins(cents, coins):
if cents < 1:
return 0
num_coins = 0
for coin in coins:
num_coins += int(cents / coin)
cents = cents % coin
if cents == 0:
break
return num_coins
def limit_coins(cents):
cache = {}
coins = [25, 10, 1]
while len(coins) > 0:
num_coins = get_coins(cents, coins)
cache[num_coins] = coins.copy()
coins.pop(0)
print(cache)
least_number_of_coins = sorted(cache, reverse=True).pop()
return least_number_of_coins
if __name__ == "__main__":
print(limit_coins(26))
# Here's the output
# >>> {2: [25, 10, 1], 8: [10, 1], 26: [1]}
# >>> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment