Question
Python3 Code to convert from Infix notation to Postfix. Notes: - Postfix expressions will only consist of numbers (integers, reals, positive or negative) and the
Python3 Code to convert from Infix notation to Postfix.
Notes:
- Postfix expressions will only consist of numbers (integers, reals, positive or negative) and the five operators separated by spaces. You may assume a capacity of 30 for the Stack will be sufficient for any expression that your programs will be required to handle.
Process the expression from left-to-right
Algo to Follow: When you encounter a value:
o Append the value to the RPN expression
When you encounter an opening parenthesis:
o Push it onto the stack When you encounter a closing parenthesis:
o Until the top of stack is an opening parenthesis, pop operators off the stack and append them to the RPN expression
o Pop the opening parenthesis from the stack (but don't put it into the RPN expression) When you encounter an operator, o1:
o While there is an operator, o2, at the top of the stack and either o1is left-associative and its precedence is less than or equal to that of o2, or o1 is right-associative, and has precedence less than that of o2
Pop o2 from the stack and append it to the RPN expressiono Finally, push o1 onto the stack
When you get to the end of the infix expression, pop (and append to the RPN expression) all remaining operator
def infix_to_postfix(input_str): """Converts an infix expression to an equivalent postfix expression"""
"""Input argument: a string containing an infix expression where tokens are space separated. Tokens are either operators + - * / ^ parentheses ( ) or numbers Returns a String containing a postfix expression """
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started