Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need Python Code for the following task: Infix, Postfix, and Prefix notations are three different but equivalent: ways of writing expressions. In Infix notations, operators

Need Python Code for the following task:

Infix, Postfix, and Prefix notations are three different but equivalent: ways of writing expressions. In Infix notations, operators are written in-between their operands. However, in Postfix expressions, the operator comes after the operands. Assume the infix expression is a string of tokens delimited by spaces. The operator tokens are *, /, +, and -, along with the left and right parentheses ( and ). The operand tokens are the single-character identifiers A, Et, C, and so on. The following steps will produce a string of tokens in Postfix order. 1. Create an empty stack called op_stack for keeping operators. Create an empty list for output. 2. Convert the input infix string to a list by using the string method split. 3. Scan the token list from left to right. If the token is an operand, append it to the end of the output list. If the token is a left parenthesis, push it on the op_stack. If the token is a right parenthesis, pop the op_stack until the corresponding left parenthesis is removed. Append each operator to the end of the output list. If the token is an operator, *, /, +, or -, push it on the op_stack. However, first, remove any operators already on the op_stack with higher or equal precedence and append them to the output list.

When the input expression has been completely processed, check the op_stack. Any operators still on the stack can be removed and appended to the end of the output list. When the input expression has been completely processed, check the op_stack. Any operators still on the stack can be removed and appended to the end of the output list. Write a function Irifx_to_Postfix that takes an arithmetic expression in Infix notation as a parameter and returns the corresponding arithmetic expression with Postfix notation.

Sample:

Infix_to_Postfix("A + B * C + D") >>> A B C * + D + Infix_to_Postfix("( A + B ) * ( C + D)") >>> A B + C D + *

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_2

Step: 3

blur-text-image_3

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions