-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_py.py
77 lines (67 loc) · 1.9 KB
/
train_py.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
TARGET = 10
EPSILON = 0.000001
class Binary(object):
def works(self, a, b):
raise NotImplementedError()
def doIt(self, a, b):
raise NotImplementedError()
class Plus(Binary):
def works(self, a, b):
return True
def doIt(self, a, b):
return a + b
class Minus(Binary):
def works(self, a, b):
return True
def doIt(self, a, b):
return a - b
class Times(Binary):
def works(self, a, b):
return True
def doIt(self, a, b):
return a * b
class Divide(Binary):
def works(self, a, b):
return abs(b) > EPSILON
def doIt(self, a, b):
return a / b
class Power(Binary):
def works(self, a, b):
try:
self.doIt(a, b)
except:
return False
return True
def doIt(self, a, b):
return a ** b
class Reverse(Binary):
def __init__(self, other):
self.other = other
def works(self, a, b):
return self.other.works(b, a)
def doIt(self, a, b):
return self.other.doIt(b, a)
def get_binaries(exp):
binaries = [Plus(), Minus(), Reverse(Minus()), Times(), Divide(), Reverse(Divide())]
if exp:
binaries.append(Power())
binaries.append(Reverse(Power()))
return binaries
def _check(dem, binaries):
if len(dem) == 1:
return dem[0] == TARGET
for iidx, i in enumerate(dem):
for jidx, j in enumerate(dem[iidx+1:]):
for o in binaries:
if not o.works(i, j):
continue
dem[iidx] = dem[0]
dem[iidx + jidx + 1] = o.doIt(i, j)
if _check(dem[1:], binaries):
print(str(dem) + ": " + str(i) + ", " + str(j))
return True
dem[iidx] = i
dem[iidx + jidx + 1] = j
return False
def go(st, exp):
return _check(list(map(float, st)), get_binaries(exp))