-
-
Notifications
You must be signed in to change notification settings - Fork 49.4k
Updated postfix_evaluation.py to support Unary operators #8787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
5bbcee5
b4565e0
b985141
800b07a
70fd7fd
ef175af
de92865
d6874ad
b4521ea
b4f51a6
9a15148
c11b18f
b2bb0af
b0c44a6
d3f4ee8
0b1627a
51df05c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,11 @@ | ||||||
| """ | ||||||
| The Reverse Polish Nation also known as Polish postfix notation | ||||||
| or simply postfix notation. | ||||||
| https://en.wikipedia.org/wiki/Reverse_Polish_notation | ||||||
| Classic examples of simple stack implementations | ||||||
| Valid operators are +, -, *, /. | ||||||
| Each operand may be an integer or another expression. | ||||||
| Output: | ||||||
| Enter a Postfix Equation (space separated) = 5 6 9 * + | ||||||
|
|
@@ -17,52 +24,210 @@ | |||||
| Result = 59 | ||||||
| """ | ||||||
|
|
||||||
| import operator as op | ||||||
| # Defining valid unary operator symbols | ||||||
| UNARY_OP_SYMBOLS = ("-", "+") | ||||||
|
|
||||||
| # Defining valid binary operator symbols | ||||||
| BINARY_OP_SYMBOLS = ("-", "+", "*", "^", "/") | ||||||
|
|
||||||
|
|
||||||
| def parse_token(data: str) -> float | str: | ||||||
| """ | ||||||
| Converts the given data to appropriate number if it is indeed a number, else returns | ||||||
| the data as it is with a False flag. This function also serves as a check of whether | ||||||
| the input is a number or not. | ||||||
| Parameters | ||||||
| ---------- | ||||||
| data : str | ||||||
| The data which needs to be converted to the appropriate number | ||||||
| Returns | ||||||
| ------- | ||||||
| bool, int or float | ||||||
| Returns a tuple of (a, b) where 'a' is True if data is indeed a number (integer | ||||||
| or numeric) and 'b' is either an integer of a floating point number. | ||||||
| If 'a' is False, then b is 'data' | ||||||
| """ | ||||||
| try: | ||||||
| return int(data) | ||||||
| except ValueError: | ||||||
| try: | ||||||
| return float(data) | ||||||
| except ValueError: | ||||||
| if is_operator(data): | ||||||
| return data | ||||||
| msg = f"{data} is neither a number nor a valid operator" | ||||||
| raise ValueError(msg) | ||||||
tianyizheng02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
|
|
||||||
| def is_operator(token: str | float) -> bool: | ||||||
| """ | ||||||
| Checks whether a given input is one of the valid operators or not. | ||||||
| Valid operators being '-', '+', '*', '^' and '/'. | ||||||
| Parameters | ||||||
| ---------- | ||||||
| token : str | ||||||
|
||||||
| token : str | |
| token : str or float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be str | float instead of or?
Why would we want token to be a float?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each element in the token list valid_expression is passed into this function to check if it's an operator—those elements may be numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add doctests for:
- evaluate(["0"])
- evaluate(["-0"])
- evaluate(["1"])
- evaluate(["-1"])
- evaluate(["-1.1"])
- evaluate(["2", "1.9", "+", "3", "*"])
- evaluate(["2", "-1.9", "+", "3", "*"])
- evaluate(["2", "--1.9", "+", "3", "*"])
Uh oh!
There was an error while loading. Please reload this page.