Skip to content

Instantly share code, notes, and snippets.

@voith
Last active November 30, 2019 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voith/bfd628e260f61e848573490162d50e0c to your computer and use it in GitHub Desktop.
Save voith/bfd628e260f61e848573490162d50e0c to your computer and use it in GitHub Desktop.
Findall possible ways €2 can be split using coins 1p, 2p, 5p, 10p, 20p, 50p, €1 (100p) and €2 (200p)
final_target = 200
possibilities = [0] * (final_target + 1)
possibilities[0] = 1
# keeping the coins sorted is the key here
# we want to compute possibilities for lower coins first.
# This helps us to fix one particular coin and
# then find the other possibilites of the change needed
coins = [1, 2, 5, 10, 20, 50, 100, 200]
for coin in coins:
for current_target in range(coin, final_target + 1):
# possibilities[current_target - coin] --> possibilies of giving the
# remaining change keeping the coin fixed.
# also sum this up with previously computed possibilities of coins
# of lesser value then the current one
possibilities[current_target] += possibilities[current_target - coin]
print(possibilities[final_target])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment