-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Update evaluate_postfix_notations.py #8758
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 11 commits
005f9c5
4eb8123
7646a57
de04120
9a92b82
ecd9e74
3559a55
7f6a692
748eff5
816120b
9e6f36a
2779cd0
2bbac37
c4f050e
c3a7c67
29b28ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,6 +17,10 @@ def evaluate_postfix(postfix_notation: list) -> int: | |||||||||||||||||||||||||||
9 | ||||||||||||||||||||||||||||
>>> evaluate_postfix(["4", "13", "5", "/", "+"]) | ||||||||||||||||||||||||||||
6 | ||||||||||||||||||||||||||||
>>> evaluate_postfix(["2", "+"]) | ||||||||||||||||||||||||||||
2 | ||||||||||||||||||||||||||||
>>> evaluate_postfix(["5", "-"]) | ||||||||||||||||||||||||||||
-5 | ||||||||||||||||||||||||||||
>>> evaluate_postfix([]) | ||||||||||||||||||||||||||||
0 | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
|
@@ -28,18 +32,28 @@ def evaluate_postfix(postfix_notation: list) -> int: | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
for token in postfix_notation: | ||||||||||||||||||||||||||||
if token in operations: | ||||||||||||||||||||||||||||
b, a = stack.pop(), stack.pop() | ||||||||||||||||||||||||||||
if token == "+": | ||||||||||||||||||||||||||||
stack.append(a + b) | ||||||||||||||||||||||||||||
elif token == "-": | ||||||||||||||||||||||||||||
stack.append(a - b) | ||||||||||||||||||||||||||||
elif token == "*": | ||||||||||||||||||||||||||||
stack.append(a * b) | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
if a * b < 0 and a % b != 0: | ||||||||||||||||||||||||||||
stack.append(a // b + 1) | ||||||||||||||||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Currently, if there's only 1 operand on the stack but the token is not Furthermore, checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not able to commit above change requested...!! |
||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
b, a = stack.pop(), stack.pop() | ||||||||||||||||||||||||||||
if token == "+": | ||||||||||||||||||||||||||||
stack.append(a + b) | ||||||||||||||||||||||||||||
elif token == "-": | ||||||||||||||||||||||||||||
stack.append(a - b) | ||||||||||||||||||||||||||||
elif token == "*": | ||||||||||||||||||||||||||||
stack.append(a * b) | ||||||||||||||||||||||||||||
elif token == "/": | ||||||||||||||||||||||||||||
if b == 0: | ||||||||||||||||||||||||||||
raise ValueError("Invalid expression: division by zero") | ||||||||||||||||||||||||||||
stack.append(a // b) | ||||||||||||||||||||||||||||
rohan472000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
msg = f"Unrecognized {token = }" | ||||||||||||||||||||||||||||
raise ValueError(msg) | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
stack.append(int(token)) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.