-
Notifications
You must be signed in to change notification settings - Fork 0
/
placing_parentheses.py
62 lines (48 loc) · 1.43 KB
/
placing_parentheses.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
# Uses python3
import sys
def evalt(a, b, op):
a = int(a)
b = int(b)
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
else:
assert False
def min_and_max(i , j, ops, M, m):
min_value = sys.maxsize
max_value = -sys.maxsize - 1
for k in range(i, j):
a = evalt(M[i][k], M[k+1][j], ops[k])
b = evalt(M[i][k], m[k+1][j], ops[k])
c = evalt(m[i][k], M[k+1][j], ops[k])
d = evalt(m[i][k], m[k+1][j], ops[k])
min_value = min(min_value, a, b, c, d)
max_value = max(max_value, a, b, c, d)
return min_value, max_value
def get_maximum_value(dataset):
d, ops = get_digits_and_operators(dataset)
n = len(d)
m = [[sys.maxsize for y in range(n)] for x in range(n)]
M =[[-sys.maxsize -1 for y in range(n)] for x in range(n)]
for i in range(n):
m[i][i] = d[i]
M[i][i] = d[i]
for s in range(1, n):
for i in range(n-s):
j = i + s
m[i][j], M[i][j] = min_and_max(i, j, ops, M, m)
return M[0][n-1]
def get_digits_and_operators(dataset):
d = []
ops = []
for i in range(len(dataset)):
if i % 2 == 0:
d.append(dataset[i])
else:
ops.append(dataset[i])
return d, ops
if __name__ == "__main__":
print(get_maximum_value(input()))