Question: from pythonds.basic.stack import Stack def infixToPostfix(infixexpr): prec = {} prec[*] = 3 prec[/] = 3 prec[+] = 2 prec[-] = 2 prec[(] = 1 opStack
from pythonds.basic.stack import Stack
def infixToPostfix(infixexpr): prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 opStack = Stack() postfixList = [] tokenList = infixexpr.split()
for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": postfixList.append(token) elif token == '(': opStack.push(token) elif token == ')': topToken = opStack.pop() while topToken != '(': postfixList.append(topToken) topToken = opStack.pop() else: while (not opStack.isEmpty()) and \ (prec[opStack.peek()] >= prec[token]): postfixList.append(opStack.pop()) opStack.push(token)
while not opStack.isEmpty(): postfixList.append(opStack.pop()) return " ".join(postfixList)
print(infixToPostfix("A * B + C * D")) print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )"))
Modify the infix-to-postfix algorithm to handle exponentiation and modulo operations. Use the ^ symbol for exponentiation and % symbol for modulo as the input tokens for testing
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
