-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Update evaluate_postfix_notations.py #8758
Conversation
Hi, Please add |
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.
I couldn't pass the doctests. Here are two failed test cases:
**********************************************************************
File ".../evaluate_postfix_notations.py", line 18, in __main__.evaluate_postfix
Failed example:
evaluate_postfix(["4", "13", "5", "/", "+"])
Expected:
6
Got:
6.6
**********************************************************************
File ".../evaluate_postfix_notations.py", line 20, in __main__.evaluate_postfix
Failed example:
evaluate_postfix(["2", "+"])
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python3.10/doctest.py", line 1350, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.evaluate_postfix[2]>", line 1, in <module>
evaluate_postfix(["2", "+"])
File ".../evaluate_postfix_notations.py", line 46, in evaluate_postfix
raise ValueError(
ValueError: Invalid expression: insufficient operands for binary operator
**********************************************************************
1 items had failures:
2 of 5 in __main__.evaluate_postfix
***Test Failed*** 2 failures.
>>> evaluate_postfix([]) | ||
0 | ||
""" | ||
if not postfix_notation: | ||
return 0 | ||
|
||
operations = {"+", "-", "*", "/"} | ||
unary_operations = {"+"} # Unary operator(s) |
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.
unary_operations
is not currently being used.
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.
now I have made several changes, kindly look into it.
if len(stack) < 1: | ||
raise ValueError( | ||
"Invalid expression: insufficient operands for unary operator" | ||
) |
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.
This doesn't fix the problem with unary negation operators. We want to support negation, not raise an error if it's present.
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.
made several changes, look into that.
elif (True) and len(stack) < 2: | ||
operand = stack.pop() | ||
stack.append(operand) |
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.
What is the purpose of this elif
block?
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.
This block will handle +,*,/ operators as above block(if block) is handling "-" operator.
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.
I guess that way is more readable.
if token == "-" and len(stack) < 2: | ||
operand = stack.pop() | ||
stack.append(-operand) | ||
elif (True) and len(stack) < 2: |
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.
elif (True) and len(stack) < 2
can be simplified to just len(stack) < 2
.
Co-authored-by: AmirSoroush <114881632+amirsoroush@users.noreply.github.com>
@cclauss, look into it again in order to merge. |
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.
Doing this work without using https://docs.python.org/3/library/operator.html seems suboptimal.
We should be able to deal with both floats and ints.
Please add tests for negative numbers, zeros, floats and
-
evaluate_postfix(["5"])
-
evaluate_postfix(["-"])
-
evaluate_postfix(["5", "A"])
-
evaluate_postfix(["5", "4", "A"])
-
evaluate_postfix(["A"])
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
for |
@rohan472000 See my comment above. It should raise an error because the negation operator has nothing to negate. |
if len(stack) < 2: | ||
operand = stack.pop() | ||
if token == "-": | ||
operand = -operand | ||
else: | ||
raise ValueError(f"Unrecognized {token = }") | ||
stack.append(operand) |
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.
if len(stack) < 2: | |
operand = stack.pop() | |
if token == "-": | |
operand = -operand | |
else: | |
raise ValueError(f"Unrecognized {token = }") | |
stack.append(operand) | |
if token == "-": | |
if len(stack) == 1: | |
operand = stack.pop() | |
stack.append(-operand) | |
elif len(stack) == 0: | |
raise ValueError(f"Negation operator with no operand") |
Currently, if there's only 1 operand on the stack but the token is not -
, then it just appends the operand back onto the stack. This effectively leaves the stack exactly as it already was, so you can skip that altogether.
Furthermore, checking len(stack) < 2
includes the case in which the stack is empty. This should raise an error since there's no operand to negate.
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.
I'm not able to commit above change requested...!!
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
@tianyizheng02 ,as your suggestion is making the build fail(doctest not passing), so I have again pushed my previous code. |
The build was failing because this test was failing: >>> evaluate_postfix(["2", "+"])
I believe my suggestion was still correct, but you need to add an if-statement to handle this edge case as well. This test should raise an exception because |
ok...but main concern is for |
I believe that test should've already been passing. Again, my suggestion (specifically the |
Issue has already been fixed by another PR |
Describe your change:
Fixes #8754
Checklist:
Fixes: {#8754}
.