-
Notifications
You must be signed in to change notification settings - Fork 0
/
ONP.py
41 lines (27 loc) · 882 Bytes
/
ONP.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
try:
T = int(input())
except:
T = 0
operator = {'+':1, '-':1, '*':2, '/':2, '^':3}
for _ in range(T):
exp = list(str(input()))
res = []
stack = []
for i in range(len(exp)):
if exp[i].isalpha():
res.append(exp[i])
if exp[i] == '(':
stack.append(exp[i])
elif exp[i] == ')':
while stack[-1] != '(':
res.append(stack.pop())
stack.pop()
elif exp[i] in operator:
if stack[-1] == '(':
stack.append(exp[i])
elif operator[exp[i]] <= operator[stack[-1]]:
while operator[exp[i]] <= operator[stack[-1]]:
res.append(stack.pop())
elif operator[exp[i]] > operator[stack[-1]]:
stack.append(exp[i])
print(*res, sep='')