-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1918.py
38 lines (32 loc) · 923 Bytes
/
1918.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
def grouping(N):
flag = False
for operator in [['*', '/'], ['+', '-']]:
tmp = []
for n in N:
tmp.append(n)
if flag:
a, b, c = tmp.pop(), tmp.pop(), tmp.pop()
tmp.append('(' + c + b + a + ')')
flag = False
if n in operator:
flag = True
N = [tmp[1]] if len(tmp) == 3 else tmp
return ''.join(N)
N = ['('] + list(input()) + [')']
stack = []
for n in N:
stack.append(n)
if n == ')':
tmp = [stack.pop()]
while tmp[-1] != '(':
tmp.append(stack.pop())
stack.append(grouping(list(reversed(tmp))))
N, stack = ''.join(stack), []
for n in N:
stack.append(n)
if n == ')':
stack.pop()
right, root, left = stack.pop(), stack.pop(), stack.pop()
stack.pop()
stack.append(left + right + root)
print(''.join(stack))