Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions