-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Conversation
… point numbers Fixes #8754 and #8724 Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py Signed-off-by: Arijit De <[email protected]>
for more information, see https://pre-commit.ci
@arijitde92 Put Fixes #8754 and #8724 in the pull request description |
… point numbers. Fixes #8754 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 Signed-off-by: Arijit De <[email protected]>
… 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 <[email protected]>
Signed-off-by: Arijit De <[email protected]>
…et_number function as it was converting float values to int, resulting in data loss. Fixes #8754 and #8724 Signed-off-by: Arijit De <[email protected]>
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? |
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.
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. |
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.
Some minor tweaks, but otherwise LGTM
|
||
Parameters | ||
---------- | ||
token : str |
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.
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.
…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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes understood. I have made the necessary changes.
|
||
Parameters | ||
---------- | ||
token : str |
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.
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.
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", "*"])
…ms#8787) * Updated postfix_evaluation.py to support Unary operators and floating point numbers Fixes TheAlgorithms#8754 and TheAlgorithms#8724 Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py Signed-off-by: Arijit De <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated postfix_evaluation.py to support Unary operators and floating point numbers. Fixes TheAlgorithms#8754 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 TheAlgorithms#8724 and made sure it passes doctest Signed-off-by: Arijit De <[email protected]> * Fixed return type hinting required by pre commit for evaluate function Signed-off-by: Arijit De <[email protected]> * Changed line 186 to return only top of stack instead of calling the get_number function as it was converting float values to int, resulting in data loss. Fixes TheAlgorithms#8754 and TheAlgorithms#8724 Signed-off-by: Arijit De <[email protected]> * Made the requested changes Also changed the code to make the evaluate function first convert all the numbers and then process the valid expression. * Fixes TheAlgorithms#8754, TheAlgorithms#8724 Updated postfix_evaluation.py 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 TheAlgorithms#8724. Added a doctest example with unary operator. * Fixes TheAlgorithms#8754, TheAlgorithms#8724 Updated postfix_evaluation.py 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 TheAlgorithms#8724. Added a doctest example with unary operator. * Fixes TheAlgorithms#8754, TheAlgorithms#8724 Updated the parse_token function of postfix_evaluation.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 TheAlgorithms#8724. Added a doctest example with unary operator and invalid expression. * Fixes TheAlgorithms#8754, TheAlgorithms#8724 Updated postfix_evaluation.py 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 TheAlgorithms#8724. Added a doctest example with unary operator and invalid expression. * Update postfix_evaluation.py * Update postfix_evaluation.py * Update postfix_evaluation.py * Update postfix_evaluation.py * Update postfix_evaluation.py --------- Signed-off-by: Arijit De <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
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.