def postfix_evaluate(items): When arithmetic expressions are given in the familiar infix notation 2 + 3* 4, we need to use parentheses to force a different evaluation order than the usual PEMDAS order determined by precedence and associativity. Writing arithmetic expressions in postfix notation (also known as Reverse Polish Notation) may look strange to uk humans accustomed to the conventional infix notation, but is computationally far easier to handle, since postfix notation allows any evaluation order to be expressed unambiguously without using any parentheses at all! A postfix expression is given as a list of items that can be either individual integers or one of the strings '+','-', '* and '/' for the four possible arithmetic operators.
def postfix_evaluate(items): When arithmetic expressions are given in the familiar infix notation 2 + 3* 4, we need to use parentheses to force a different evaluation order than the usual PEMDAS order determined by precedence and associativity. Writing arithmetic expressions in postfix notation (also known as Reverse Polish Notation) may look strange to uk humans accustomed to the conventional infix notation, but is computationally far easier to handle, since postfix notation allows any evaluation order to be expressed unambiguously without using any parentheses at all! A postfix expression is given as a list of items that can be either individual integers or one of the strings '+','-', '* and '/' for the four possible arithmetic operators.
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
Related questions
Question
![Postfix interpreter
def postfix_evaluate(items):
When arithmetic expressions are given in the familiar infix notation 2 + 3 * 4, we need to use
parentheses to force a different evaluation order than the usual PEMDAS order determined by
precedence and associativity. Writing arithmetic expressions in postfix notation (also known as
Reverse Polish Notation) may look strange to us humans accustomed to the conventional infix
notation, but is computationally far easier to handle, since postfix notation allows any evaluation
order to be expressed unambiguously without using any parentheses at all! A postfix expression is
given as a list of items that can be either individual integers or one of the strings '+','-
and '/' for the four possible arithmetic operators.
To evaluate a postfix expression using a simple linear loop, use a list as a stack that is initially
empty. Loop through the items one by one, in order from left to right. Whenever the current item
is an integer, just append it to the end of the list. Whenever the current item is one of the four
arithmetic operations, pop two items from the end of the list, perform that operation on those
items, and append the result to the list. Assuming that items is a legal postfix expression, which is
guaranteed in this problem so that you don't need to do any error handling, once all items have been
processed this way, the one number that remains in the stack is returned as the final answer.
To avoid the intricacies of floating point arithmetic, you should perform the division operation using
the Python integer division operator // that truncates the result to the integer part. Furthermore,
to avoid the crash caused by dividing by zero, in this problem we shall artificially make up a rule
that dividing anything by zero will simply evaluate to zero instead of crashing.
items
(Equivalent infix) Expected result
[2, 3, '+', 4,
*']
(2+3) * 4
20
[2, 3, 4,
*+'1
2 + (3*4)
14
[3, 3, 3, '-', '/']
3/ (3 - 3)
[7, 3, '/'1
7/3
2
[1, 2, 3, 4, 5, 6,
1*2*3*4*5 * 6
720](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0f4c3b8d-420b-4827-8129-a9e5c9a665e1%2Ff297aca2-939d-4e66-9dda-52da9128a6ae%2Foq8j5z_processed.png&w=3840&q=75)
Transcribed Image Text:Postfix interpreter
def postfix_evaluate(items):
When arithmetic expressions are given in the familiar infix notation 2 + 3 * 4, we need to use
parentheses to force a different evaluation order than the usual PEMDAS order determined by
precedence and associativity. Writing arithmetic expressions in postfix notation (also known as
Reverse Polish Notation) may look strange to us humans accustomed to the conventional infix
notation, but is computationally far easier to handle, since postfix notation allows any evaluation
order to be expressed unambiguously without using any parentheses at all! A postfix expression is
given as a list of items that can be either individual integers or one of the strings '+','-
and '/' for the four possible arithmetic operators.
To evaluate a postfix expression using a simple linear loop, use a list as a stack that is initially
empty. Loop through the items one by one, in order from left to right. Whenever the current item
is an integer, just append it to the end of the list. Whenever the current item is one of the four
arithmetic operations, pop two items from the end of the list, perform that operation on those
items, and append the result to the list. Assuming that items is a legal postfix expression, which is
guaranteed in this problem so that you don't need to do any error handling, once all items have been
processed this way, the one number that remains in the stack is returned as the final answer.
To avoid the intricacies of floating point arithmetic, you should perform the division operation using
the Python integer division operator // that truncates the result to the integer part. Furthermore,
to avoid the crash caused by dividing by zero, in this problem we shall artificially make up a rule
that dividing anything by zero will simply evaluate to zero instead of crashing.
items
(Equivalent infix) Expected result
[2, 3, '+', 4,
*']
(2+3) * 4
20
[2, 3, 4,
*+'1
2 + (3*4)
14
[3, 3, 3, '-', '/']
3/ (3 - 3)
[7, 3, '/'1
7/3
2
[1, 2, 3, 4, 5, 6,
1*2*3*4*5 * 6
720
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT

C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr

C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT

C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr

New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning

Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage