-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.py
executable file
·81 lines (63 loc) · 2.01 KB
/
numbers.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
78
79
80
#!/usr/bin/env python3
import sys
import itertools
numbers = [int(x) for x in sys.argv[1:7]]
target = int(sys.argv[7])
print("Numbers to use: {}".format(" ".join([str(x) for x in numbers])))
print("Target result : {}".format(target))
class add():
def __init__(self, a, b):
self.a = a
self.b = b
self.value = a + b
def __str__(self):
return "{} + {} = {}".format(self.a, self.b, self.value)
class subtract():
def __init__(self, a, b):
self.a = a
self.b = b
self.value = a - b
def __str__(self):
return "{} - {} = {}".format(self.a, self.b, self.value)
class multiply():
def __init__(self, a, b):
self.a = a
self.b = b
self.value = a * b
def __str__(self):
return "{} * {} = {}".format(self.a, self.b, self.value)
class divide():
def __init__(self, a, b):
self.a = a
self.b = b
self.value = a / b if b != 0 else 0.0
if self.value.is_integer():
self.value = int(self.value)
else:
self.value = 0
def __str__(self):
return "{} / {} = {}".format(self.a, self.b, self.value)
operations = [add, subtract, multiply, divide]
final = None
def do(numbers, previous):
global final
for i,v in enumerate(numbers):
remaining = numbers[:i] + numbers[i+1:]
for ii,vv in enumerate(remaining):
if final is not None and len(previous) + 1 >= len(final):
break
remaining_inner = remaining[:ii] + remaining[ii+1:]
for op in operations:
result = op(v, vv)
progress = previous + [result]
if result.value < 1:
continue
if result.value == target:
final = progress
return True
if do([result.value] + remaining_inner, progress):
break
do(numbers, [])
if final is not None:
print("")
print("\n".join([str(x) for x in final]))