24点计算器 by Python (带去重)

介绍

直接写过程了,代码中有注释,去重写的十分烂,之后再去优化吧。

脚本

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import itertools
import sys
import re

ops = ['+', '-', '*', '/']


def input_card():
if len(sys.argv) == 1:
cards = list(map(int, input('Input four card numbers: ').split(' ')))
else:
cards = sys.argv[1:]
try:
assert len(cards) == 4
except AssertionError:
print('Need four cards!')
exit(0)

return cards


def solve(cards):
orders = ['(({}{}{}){}{}){}{}', '({}{}({}{}{})){}{}',
'{}{}(({}{}{}){}{})', '{}{}({}{}({}{}{}))', '({}{}{}){}({}{}{})']
result = []
for card in itertools.permutations(cards, r=4):
for op in itertools.product(ops, repeat=3):
strings = []
for order in orders:
strings.append(order.format(
card[0], op[0], card[1], op[1], card[2], op[2], card[3]))

for string in strings:
try:
ret = eval(string)
except ZeroDivisionError:
continue

if round(ret, ndigits=1) == 24:
result.append(string)

return result


def deduplication(r):
return list(set(r))


def delete(data, index):
index = list(set(index))
index.sort(reverse=True)

for i in index:
del data[i]


def improve(result):
result = deduplication(result)

# 去第一次括号去重
tmp1 = []
index = []
for s in result:
ret = re.search('\([^()]*\)', s).group()
tmp1.append(s.replace(ret, str(eval(ret))))

for i, t in enumerate(tmp1):
for j, tt in enumerate(tmp1[i + 1:]):
if t == tt:
index.append(i + j + 1)

# 去第二次括号去重
tmp2 = []
for s in tmp1:
ret = re.search('\([^()]*\)', s).group()
tmp2.append(s.replace(ret, str(eval(ret))))

for i, t in enumerate(tmp2):
for j, tt in enumerate(tmp2[i + 1:]):
if t == tt:
index.append(i + j + 1)

# 左右对称去重
for i, t in enumerate(tmp2):
for j, tt in enumerate(tmp2[i + 1:]):
loc = re.search('[+\-*/]{1}', tt).start()
tt = tt[loc + 1:] + tt[loc] + tt[:loc]
if tt == t:
index.append(i + j + 1)

delete(result, index)

return result


def main():

cards = input_card()
result = solve(cards)
result = improve(result)
for x in result:
print(x)


if __name__ == '__main__':
main()