-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfives.py
31 lines (26 loc) · 935 Bytes
/
fives.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
def add (a, b):
return a + b
def subtract (a, b):
return a - b
def multiply (a, b):
return a * b
def divide (a, b):
if b == 0:
return None
else:
return a/b
def search (depth):
"""Returns a dictionary whose keys are integers and whose values are the
minumum number of 5s needed to make the key using +-*/."""
results = {5:1}
newresults = {}
for i in range(depth):
for k1, v1 in results.iteritems():
for k2, v2 in results.iteritems():
for f in [add, subtract, multiply, divide]:
newkey = f(k1, k2)
newval = v1 + v2
if not newkey in results and newkey != None and (not newkey in newresults or newresults[newkey] > newval) and newval<=depth:
newresults[newkey] = newval
results = dict(newresults.items() + results.items())
print results.items()