-
Notifications
You must be signed in to change notification settings - Fork 0
/
24solver.py
64 lines (60 loc) · 2.16 KB
/
24solver.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
'''
Do you know the 24 card game?
'''
from itertools import permutations, combinations
def solve(numbers):
'''Recursive. '''
if len(numbers) == 1:
if abs(numbers[0] - 24) < .00001:
return True, [24]
return False, []
for operations, iterPolicies in (
('+*', combinations), ('-/', permutations)
):
for a, b in iterPolicies(numbers, 2):
for operation in operations:
numbers_copy = numbers.copy()
[numbers_copy.remove(x) for x in (a, b)]
expression = f'{a}{operation}{b}'
try:
new_number = eval(expression)
new_number = tryRounding(new_number)
except ZeroDivisionError:
continue
except ValueError:
pass
if new_number >= 0:
can_do, solution = solve(numbers_copy + [new_number])
did_it = False
if can_do:
new_solution = []
for token in solution:
if not did_it and type(token) in (int, float) and abs(token - new_number) < .0001:
new_solution.extend(['(', a, operation, b, ')'])
did_it = True
else:
new_solution.append(token)
return True, new_solution
return False, []
def tryRounding(x):
rounded = round(x)
if abs(rounded - x) < .0001:
return rounded
raise ValueError
def main():
while True:
try:
op = input('Four numbers, seperated by space: ')
except (EOFError, KeyboardInterrupt):
break
numbers = [int(x.strip()) for x in op.strip().split(' ')]
can_do, path = solve(numbers)
if can_do:
print('There is solution. ')
input('Press ENTER to see it...')
print(''.join([str(x) for x in path]).replace('*', '×').replace('/', '÷'))
else:
print('No solution.')
print()
if __name__ == "__main__":
main()