Question

Python The final value of the evaluation of the postfix expression 6 2 + 5 *...

Python

The final value of the evaluation of the postfix expression 6 2 + 5 * 8 4 / - is ----------

Homework Answers

Answer #1

Algorithm for Postfix Evaluation

  • We create a stack to store values
  • Read the expression in a loop and do the following
    • If itemis an operand (number) then push it in the stack we created
    • If item is an operator, pop operands for the operator from the stack, evaluate them and push the evaluated value back into the stack.
  • In the end then number in the stack is the final answer

Progarm code in Python

Program Sample Output Screenshot

Program Code to copy

# Class to evaluate the expression
class PostfixEvaluation:

    # Constructor to initialize the class variables
    def __init__(self):
        self.top = -1
        # declare a stack
        self.array = []

    # check if the stack is empty
    def isEmpty(self):
        return True if self.top == -1 else False

    # Return item on stack  top
    def peek(self):
        return self.array[-1]

    # Pop item from the stack
    def pop(self):
        if not self.isEmpty():
            self.top -= 1
            return self.array.pop()
        else:
            return "$"

    # Push item into the stack
    def push(self, op):
        self.top += 1
        self.array.append(op)

    # This function will evaluate the postfix expression

    def evaluatePostfixExpression(self, expression):

        # Iterate over items in the expression array
        for item in expression:

            # push in the stack if it is an operand
            if item.isdigit():
                self.push(int(item))

            # if this is an operator then pop operands from stack and
            # evaluate and push evaluated value back in stack
            else:
                val1 = str(self.pop())
                val2 = str(self.pop())
                self.push(int(eval(val2 + item + val1)))

        return self.pop()


# Driver program to test above function
expression = "6 2 + 5 * 8 4 / -"
evaluater = PostfixEvaluation()
value = evaluater.evaluatePostfixExpression(expression.split())
print("The final value of the evaluation of the postfix expression %s is  %d" %
      (expression, value))
Know the answer?
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
Similar Questions
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
(For Python) Evaluating Postfix Arithmetic Expressions. In this project you are to implement a Postfix Expression...
(For Python) Evaluating Postfix Arithmetic Expressions. In this project you are to implement a Postfix Expression Evaluator as described in section 7-3b of the book. The program should ask the user for a string that contains a Postfix Expression. It should then use the string's split function to create a list with each token in the expression stored as items in the list. Now, using either the stack classes from 7.2 or using the simulated stack functionality available in a...
For the postfix expressions, 32 5 3 + / 5 *, trace the algorithm for evaluating...
For the postfix expressions, 32 5 3 + / 5 *, trace the algorithm for evaluating postfix expressions by showing the contents of the stack immediately before each of the tokens marked with a caret is read. Also, give the value of the postfix expression.
Please give the postfix form of the arithmetic expression (a*b + 2*c)/(2 - 3*d) + c
Please give the postfix form of the arithmetic expression (a*b + 2*c)/(2 - 3*d) + c
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
Write a Python expression that uses the list even_numbers below and evaluates to the list [4]....
Write a Python expression that uses the list even_numbers below and evaluates to the list [4]. or produces the list to [4] even_numbers = [0, 2, 4, 6, 8, 10, 12]
python Given: x = 3 y = 2 Evaluate the following expression to a single numeric...
python Given: x = 3 y = 2 Evaluate the following expression to a single numeric answer: (x + y) // x Step 1) Substitute in the values Step 2) Perform the operation of highest precedence Step 3) Final answer
1. Use the given conditions to find the exact value of the expression. sin(α) = -5/3,...
1. Use the given conditions to find the exact value of the expression. sin(α) = -5/3, tan(α) > 0, sin(α - 5π/3) 2. Use the given conditions to find the exact value of the expression. cos α = 24/25, sin α < 0, cos(α + π/6) 3. Use the given conditions to find the exact value of the expression. cot x = √3, cos x < 0, tan(x + π/6) 4. If α and β are acute angles such that...
python Given: age1 = 21 age2 = 14 age3 = 15 Simplify the following expression to...
python Given: age1 = 21 age2 = 14 age3 = 15 Simplify the following expression to true or false: age1 > age3 and not(age3 == 15) or age3 > age2 Step 1) Substitute in the values Step 2) Evaluate all the relational operators (<, >, <=, >=, ==, !=) to true or false Step 3) Evaluate the boolean (logical) operator (and, or, not) of highest precedence Step 4) Evaluate the next boolean (logical) operator (and, or, not) of highest precedence...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D list in an N X N structure with integers 1-(N*N) Print the list created Reverse the list such that (1) each list in the 2D list is reversed and (2) the order of each list in the outer list is reversed (see example) Print the reversed list Example Execution What size 2D list would you like to create? N> 3 The original list is:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT