Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Notes: To break up a string into tokens, consider using the str.split method. To convert a token (string) into a number (float), consider using try/except

Notes:

To break up a string into tokens, consider using the str.split method.

To convert a token (string) into a number (float), consider using try/except blocks along with the float function.

Your functions will support the mathematical operations of +, -, *, /, //, and ^.

That last one, ^, means exponentiation. It has higher precedence than the other operators (e.g., 2 * 4 ^ 3 == 2 * 64 == 128) and is right-associative (e.g., 2 ^ 3 ^ 2 == 2 ^ (3 ^ 2) == 2 ^ 9 == 512). (Note that ^ is not how Python does exponentiation. Thats OK! This is our calculator, we can do what we want!)

All the other operators are left-associative (e.g.,2 * 3 // 4 == (2 * 3) // 4 == 6 // 4 == 1).

At no point should you ever be using the Python builtin function eval.

image text in transcribed

please use the given outline below for exp_eval.py

please contain docstrings explaining purposes

#exp_eval.py

from __future__ import annotations

# NOTE: You'll probably need to import some things from array_stack. def postfix_eval(input_string: str) -> float: """Evaluate the given RPN expression.

Args: input_string: an RPN expression

Returns: The result of the expression evaluation

Raises: ValueError: if the input is not well-formed ZeroDivisionError: if the input would cause division by zero """

3 Evaluating a Postfix (RPN) Expression 3.1 Algorithm In a file called exp_eval.py, you will implement this algorithm as a function called postfix_eval. While RPN will look strange until you are familiar with it, here you can begin to see some of its advantages for programmers. One such advantage of RPN is that it removes the need for parentheses. Infix notation supports operator precedence (* and / have higher precedence than + and ) and thus needs parentheses to override this precedence. This makes parsing such expressions much more difficult. RPN has no notion of precedence, the operators are processed in the order they are encountered. This makes evaluating RPN expressions fairly straightforward and is a perfect application for a stack data structure, we just follow these steps: - Process the expression from left to right - When a value is encountered: - Push the value onto the stack - When an operator is encountered: - Pop the required number of values from the stack - Perform the operation - Push the result back onto the stack - Return the last value remaining on the stack For example, given the expression 512+4+3 You may (and should) use the Python string method str.split to separate the string into tokens. 3.2 Exceptions You may (and should) assume that a string is always passed to post fix_eval. However, that does not mean that the RPN expression will always be valid. Specifically, your function should raise a ValueError with the following messages in the following conditions: - "empty input" if the input string is an empty string - "invalid token" if one of the tokens is neither a valid operand not a valid operator (e.g., 2 a +) - "insufficient operands" if the expression does not contains sufficient operands (e.g., 2 +) - "too many operands" if the expression contains too many operands (e.g., 2 3 4 +) To raise an exception with a message, you will raise ValueError("your message here"). Additionally, if you would divide by zero, your code should raise a ZeroDivisionError

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions