Question

Python Assignment: Write a program that uses stacks to solve postfix equations.Read the below in a...

Python Assignment: Write a program that uses stacks to solve postfix equations.Read the below in a strings, character by character, into a stack and solve them. 3 2 + 4 * 5 1 - /

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey here is answer to your question.

class Stack:
def __init__(self):
self.items = []

def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return (self.items == [])
def cal_postfix(s,string):
#loop over expression
for i in string:
#if its a digit then push to stack
if i.isdigit():
s.push(i)
           # if its not a digit then pop last 2 values and
           # eval the expression and push that to stack
else:
val1 = s.pop()
val2 = s.pop()
s.push(str(eval(val2 + i + val1)))
       # do that for whole string at last pop final value
return s.pop()
exp = "32+4*51-/"
# make a stack
s = Stack()

# call function with args as stack and expression
print(cal_postfix(s,exp))

In case of any doubt please comment. Happy Learning :)

Add a comment
Know the answer?
Add Answer to:
Python Assignment: Write a program that uses stacks to solve postfix equations.Read the below in 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
  • Write a program to convert an expression written in infix notation to be converted to postfix...

    Write a program to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b. Use stacks...

  • C++ Stack Program Write a program that uses stacks to evaluate an arithmetic expression in infix...

    C++ Stack Program Write a program that uses stacks to evaluate an arithmetic expression in infix notation. The program should NOT convert the infix to postfix and then evaluate the postfix. The program takes as input a numeric expression in infix notation, such as 3+4*2, and outputs the result. 1a)   Operators are +, -, *, / 1b) Assume that the expression is formed correctly so that each operation has two arguments. 1c) The expression can have parenthesis, for example: 3*(4-2)+6...

  • 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...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • 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...

  • Use C++ Language to program evaluation using Prefix and Postfix and use stacks and queues Math...

    Use C++ Language to program evaluation using Prefix and Postfix and use stacks and queues Math expression and converts the same to its forms. Infix: 3 + 2 - 5 * 8 + 3 Prefix: - + 3 2 + * 5 8 3 Postfix: 3 2 + 5 8 * - 3 +

  • 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 /...

  • write a java program that takes infix notation equations and changes it to postfix notation. you...

    write a java program that takes infix notation equations and changes it to postfix notation. you will have to solve the postfix equation after. (make sure you are making your own stack class)

  • Please help with this python assignment. Thank you. question 1 Write a Python program to read...

    Please help with this python assignment. Thank you. question 1 Write a Python program to read a file line by line store it into a variable. question 2 Write a Python program to read a file line by line store it into an array. question 3 Write a python program to find the longest words. question 4 Write a Python program to count the number of lines in a text file. question 5 Write a Python program to count the...

  • Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user....

    Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...

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