Question

Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

Python Issue

Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands.

The algorithm to evaluate any postfix expression is based on stack and is pretty simple:

Initialize empty stack

For every token in the postfix expression (scanned from left to right):

If the token is an operand (number), push it on the stack

Otherwise, if the token is an operator (or function):

Check if the stack contains the sufficient number of values (usually two) for given operator

If there are not enough values, finish the algorithm with an error

Pop the appropriate number of values from the stack

Evaluate the operator using the popped values and push the single result on the stack

Return the final result of the calculation

Implement a function def eval_postfix(expression_str) that evaluates and returns the result of a postfix expression. The postfix expression to be evaluated is passed to the function as a string parameter. Each component of the postfix expression string is separated by a single space. Your function needs to support the following operations: "+", "-", "*", "/", "%" and "**". Your function should also work with values consisting of more than one digit.

For example:

Test Result
print(eval_postfix("2 3 * 4 +"))
10
print(eval_postfix("2 3 4 + *"))
14
0 1
Add a comment Improve this question Transcribed image text
Answer #1

def evaluate_postfix(input_string):


    stack = list()                                      # initializing the empty stack

    for i in input_string:

        result = 0;                                     #result variable for storing the result

        if i in "0123456789":                            #checking whether input symbol is number or not

            stack.append(int(i))

        elif stack:                                     #checking whether stack is not empty

            operand1=stack.pop()                        # removing the top value of the stack

            operand2=stack.pop()                         #removing the next top value of the stack

            if i == "+":

                result = operand1+operand2

            elif i == "-":

                result = operand2 - operand1

            elif i == "*":

                result = operand1 * operand2

            elif i == "/":

                result = operand2 / operand1

            elif i == "^":                              #taking raise power as ^ operator

                result = operand2 ** operand1

            elif i == "%":

                result= operand2 % operand1

        if result!=0:                                   ##checking result is zero or not

            stack.append(result)                        ## if any result we are ushing it into stcak

    return stack.pop()                                   ## returning the final stack value

input_string=raw_input("enter the postfix string\n");   #taking the expression string as input

final_val=evaluate_postfix(input_string)                #calling the function

print final_val                                         #printing the final result
  

Add a comment
Know the answer?
Add Answer to:
Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

    By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

    In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is  62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)

    Your program must evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN). + (addition), - (subtraction), * (multiplication) and / (division) should be treated as arithmetic operations. Depending on the selection, numerical values must be entered and displayed in decimal, hexadecimal or binary form. Example: The expression ((3+4) *(7+1.6-12) -5) / (3-8.7)Will be calculated on your RPN calculator as 3 4 + 7 1.6 + 12 - * 5 - 3 8.7 - / Your RPN computer's user...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT