Last active
November 30, 2019 18:52
-
-
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)
This file contains hidden or 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
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