Updated postfix_evaluation.py to support Unary operators#8787
Updated postfix_evaluation.py to support Unary operators#8787cclauss merged 17 commits intoTheAlgorithms:masterfrom arijitde92:master
Conversation
for more information, see https://pre-commit.ci
|
@arijitde92 Put Fixes #8754 and #8724 in the pull request description |
… point numbers. Fixes #8754, #8724 and formatted code to pass ruff and black test. Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py which fixes #8724 and made sure it passes doctest. Also updated type hinting which was required by pre-commit Signed-off-by: Arijit De <arijitde2050@gmail.com>
Signed-off-by: Arijit De <arijitde2050@gmail.com>
|
Hi @CaedenPH , @amirsoroush , Please review my PR and suggest changes if any. postfix_evaluation.py now works with unary operators and floating point numbers. |
|
Seems like a lot more code to solve the same problem. Is it faster? |
This comment was marked as off-topic.
This comment was marked as off-topic.
Hi @cclauss , I am new to open source contribution. Please guide me on what else I can do to make the code shorter or faster. |
|
Hi @CaedenPH @cclauss @darkstar @amirsoroush, is there anything else I can do to improve the code? Could you please review the PR and let me know if any changes are required? |
1 similar comment
|
Hi @CaedenPH @cclauss @darkstar @amirsoroush, is there anything else I can do to improve the code? Could you please review the PR and let me know if any changes are required? |
tianyizheng02
left a comment
There was a problem hiding this comment.
General feedback: pay attention to where you can invert conditions or use early breaks/returns in order to simplify the control flow and avoid heavily nested code
Also changed the code to make the evaluate function first convert all the numbers and then process the valid expression.
|
Hi @tianyizheng02 , I have made the requested changes. Please check and let me know if it is okay. Thanks. |
tianyizheng02
left a comment
There was a problem hiding this comment.
Some minor tweaks, but otherwise LGTM
|
|
||
| Parameters | ||
| ---------- | ||
| token : str |
There was a problem hiding this comment.
| token : str | |
| token : str or float |
There was a problem hiding this comment.
Would it be str | float instead of or?
Why would we want token to be a float?
There was a problem hiding this comment.
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.
…tion.py ostfix_evaluation.py now supports Unary operators and floating point numbers. Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py which fixes #8724. Added a doctest example with unary operator and invalid expression.
| def parse_token(token: str | float) -> 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 | ||
| ---------- | ||
| token : str or float |
There was a problem hiding this comment.
| def parse_token(token: str | float) -> 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 | |
| ---------- | |
| token : str or float | |
| def parse_token(token: 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 | |
| ---------- | |
| token : str |
I meant for this suggestion to be for is_operator, not this function. I believe this function is only used when initially parsing the input list, so all tokens passed into this function should be strings.
There was a problem hiding this comment.
Yes understood. I have made the necessary changes.
|
|
||
| Parameters | ||
| ---------- | ||
| token : str |
There was a problem hiding this comment.
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.
postfix_evaluation.py now supports Unary operators and floating point numbers. Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py which fixes #8724. Added a doctest example with unary operator and invalid expression.
| Function that evaluates postfix expression using a stack. | ||
| >>> evaluate(["2", "1", "+", "3", "*"]) | ||
| 9.0 | ||
| >>> evaluate(["4", "13", "5", "/", "+"]) |
There was a problem hiding this comment.
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", "*"])
Fixes #8754
Fixes #8724
Describe your change:
Fixes Need to support for unary operator in postfix evaluation. #8754 and Duplicate solutions for postfix notation evaluation. #8724
Checklist:
Fixes: #{$ISSUE_NO}.Updated postfix_evaluation.py to support Unary operators and floating point numbers. Fixes #8754.
Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py which fixes #8724.