forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINPSTFIX.py
34 lines (31 loc) · 923 Bytes
/
INPSTFIX.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
def fun(n, eq):
eq = [i for i in eq]
ans = []
stack = []
precedence = {'(':0,'+':1,'-':1,'*':2,'/':2,'^':3}
for i in eq:
if i == '(':
stack.append(i)
elif i >= 'A' and i <= 'Z':
ans.append(i)
elif i == ')':
while len(stack)> 0 and stack[-1] != '(':
ans.append(stack.pop())
stack.pop()
elif i in '+-*/^':
if len(stack)==0 or stack[-1] == '(':
stack.append(i)
else:
while len(stack)> 0 and precedence[i] <= precedence[stack[-1]]:
ans.append(stack.pop())
else:
stack.append(i)
for i in stack[::-1]:
if i != '(':
ans.append(i)
res = ''
for i in ans:
res += i
return res
for testcase in range(int(input())):
print(fun(int(input()), input()))