-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathONP.py
40 lines (37 loc) · 954 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
# Problem Description: https://www.spoj.com/problems/ONP/
def priority(op):
if(op == "^"):
return 5
elif(op == "/"):
return 4
elif(op == "*"):
return 3
elif(op == "-"):
return 2
elif(op == "+"):
return 1
else:
return 0
t = int(input())
for i in range(t):
exp = input()
stck = []
res = []
for token in exp:
if(token == "("):
stck.append("(")
elif(token == ")"):
temp = stck.pop()
while(temp != "("):
res.append(temp)
temp = stck.pop()
elif((token >= "a" and token <= "z") or
(token >= "A" and token <= "Z")):
res.append(token)
else:
if(priority(stck[-1]) <= priority(token)):
stck.append(token)
else:
res.append(stck.pop())
stck.append(token)
print("".join(res))