Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using python3 to solve the problem Reverse Polish Nouation (RPN), also known as postfix notation, is an alternate way to represent a sequence of arithmetic

image text in transcribedimage text in transcribed

using python3 to solve the problem

Reverse Polish Nouation (RPN), also known as postfix notation, is an alternate way to represent a sequence of arithmetic operations. In RPN, we write the operands/values first, followed by the applicable arithmetic operator (for example, 5 2 would be written as 524). To evaluate a postfix/RPN expression, we store each operand that we see, in order, until we reach an operator. Once we see an operator, we apply it to the last two operands we saw (in the appropriate order) and then store the result back to our list while we continue scanning for operands and operators. When we are done, we should have exactly one value in the list; this is our final answer For example, consider the expression 43*5- (assume that each operand is initially one digit in length). We begin by storing 4, then 3. When we encounter the multiplication operator, we multiply our last two stored values (4 and 3) and store the result (12). We store the next operand (5). Finally, we encounter the subtraction operator, so we calculate 12-5 to get 7 In Python, we can perform this calculation using a list. Each time we read an operand from the input, we use append ) to add it to the end of our (initially empty)st. Each time we read an operator from the input, we use pop to remove and store the last two elements of the list in temporary variables. We apply the operator and append the result to the end of the list. When we are done, f the lst contains exactly one value, that value is our final answer. If it does not, then there must have been an error in the original input value. Complete the rpn function, which takes a string representing an RPN expression (with single-digit operands) as its only argument. The function returns either the answer to the expression, or it returns the string "Error" (note the capitalization) if the final internal list is empty or contains more than 1 value. You may assume that you will never receive an input string with more operators than operands, or with any characters other than digits and the four operators +-/ Note: Since we are working exclusively with integers for now, every division operator performs floor division. Thus 83/ evaluates to 2, not 2.67 Your basic solution approach will probably resemble the following pseudocode For each character in the input: If the current character is a digit: Convert it to an integer and append it to your list Otherwise (meaning the current character is an operator): Pop the last number from the list and save it to a variable op2 Pop the (new) last number from the list and save it to a variable opl Using an if-elif chain with four parts (one for each operator): Compute the result of opl operator op2 (e.g., opl - op2 for subtraction) Append the result to your list If the list has exactly on element, return that value Otherwise, return the string "Error" Hint: To identify an integer, you can either use the string method isdigit , or use the n operator to check whether the character appears in the string "1234567890" or the constant string.digits from the string module: my_character.isdigit OR my_character in string.digits Examples: eturn rpn ("532 rpn ("273-" rror

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

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago