I wanted to make an infix to prefix converter. When I ran the code, the operator in the string sends all the operators in the beginning of the returning string.
How can I fix the code below?
class Stack:
def __init__(self):
self.a = []
def isEmpty(self):
return self.a == []
def push(self,i):
self.a.append(i)
def pop(self):
return self.a.pop()
def peek(self):
return self.a[len(self.a)-1]
def infixToPrefix(s):
prec = {'/':3,'*':3,'+':2,'-':2,'^':4,'(':1}
opStack = Stack()
prefixList = []
temp = []
for token in s:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
prefixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
temp.append(topToken)
topToken = opStack.pop()
prefixList = temp + prefixList
temp = []
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()]>= prec[token]):
temp.append(opStack.pop())
prefixList = temp + prefixList
temp = []
opStack.push(token)
while not opStack.isEmpty():
temp.append(opStack.pop())
prefixList = temp + prefixList
return ''.join(prefixList)
print infixToPrefix("(A+B)*C-(D-E)*(F+G)")
while...is syntactically incorrect.-*+ABC*-DE+FG? Have you tried putting someprintstatements into yourStackmethods and strategic places through your loop so you can follow what's going on? Have you tried running through your algorithm by hand, with pencil & paper, to make sure it does what you expect it to?