-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Need to support for unary operator in postfix evaluation. #8754
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
Comments
I think there are three main ways that we can implement this, but none of them is a perfect solution:
Let me know if you've thought of any other potential solutions. |
Can you please say how it would be inconvenient to enforce 2.? We can insert 0 at the beginning of the string if the first character is not a number and continue the postfix conversion, can't we? Or am I wrong? |
For the same reason you mentioned, I'm not satisfied with the first option as we normally do not differentiate between unary minus and binary minus operator in a math expression. I think last option is reasonable and it's what @rohan472000 has suggested in #8758 . |
@niranjank2022 We're not converting the string to postfix notation. Rather, this file evaluates a postfix string, so the input is already in postfix notation, not infix notation. If there's a unary negation operator, it definitely won't be at the start of the string, so it's not immediately obvious where to insert the 0. |
@amirsoroush I think option 3 is the most reasonable as well, but I'm not completely sure if this can always be generalized to other unary operators. I believe this method would also work for unary functions such as |
* Updated postfix_evaluation.py to support Unary operators and floating 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]> * [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 #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]> * 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 #8754 and #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 #8754, #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 #8724. Added a doctest example with unary operator. * Fixes #8754, #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 #8724. Added a doctest example with unary operator. * Fixes #8754, #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 #8724. Added a doctest example with unary operator and invalid expression. * Fixes #8754, #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 #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]>
…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]>
Feature description
As mentioned in #8724, we have two files for evaluating postfix expressions. But currently both of them only work with binary operators and will raise an exception when an unary operator is given.
example:
2-3+
is a postfix notation for-2+3
infix and should result in1
.The text was updated successfully, but these errors were encountered: