Please help me in Infix to Postfix. I need some hints INPSTFIX
I suggest you to read all the rules regarding INFIX to POSTFIX conversion from Geek for Geeks or watch any video from youtube, then try implementing it.
I am getting run time error. Please help me
def op(nd):
if(nd == '^'):
return(3)
elif(nd == '/' or nd == '*'):
return(2)
elif(nd == '+' or nd == '-'):
return(1)
for i in range(int(input())):
n = int(input())
s = input()
a = []
b = []
for i in s:
if(i.isalpha()):
b.append(i)
#if(op(i) and len(a) == 0):
# a.append(i)
elif(i == '('):
a.append(i)
elif(i == ')'):
while(len(a) != 0 and a[-1] != '('):
c = a.pop()
b.append(c)
a.pop()
else:
while(len(a) != 0 and a[-1] != '(' and (op(i) <= op(a[-1]))):
c = a.pop()
b.append(c)
#else:
a.append(i)
while(len(a) != 0):
c = a.pop()
b.append(c)
b = ''.join(map(str, b))
#print(a)
print(b)
guys and one more question: do we really need to use precedence in this ? from the examples it seems that every operation is closed under parenthesis, nothing about precedence is mentioned in the question.