-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler31.py
51 lines (35 loc) · 1.21 KB
/
euler31.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
"""
def ways_to_pay(n, vals):
valid_vals = [v for v in vals if v <= n]
if n == 0:
return [dict()]
if len(valid_vals) == 0 or n < min(valid_vals):
return []
#print(f'ways_to_pay({n}, {valid_vals})')
sols = []
# either you take one unit of the top valued currencies, or you don't
# take ...
v = valid_vals[0]
remainder = n - v
cur_sols = ways_to_pay(remainder, vals)
for sol in cur_sols:
if v in sol:
sol[v] += 1
else:
sol[v] = 1
sols = sols + cur_sols
# leave ... meaning you're not allowed to pick that currency anymore
sols = sols + ways_to_pay(n, valid_vals[1:])
return sols
def main():
vals = [n*100 for n in [2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01]] # force integers to avoid precision issues
res = ways_to_pay(200, vals)
print(res)
print(len(res))
main()