-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
005f9c5
Update evaluate_postfix_notations.py
rohan472000 4eb8123
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7646a57
Update evaluate_postfix_notations.py
rohan472000 de04120
Update evaluate_postfix_notations.py
rohan472000 9a92b82
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ecd9e74
Update evaluate_postfix_notations.py
rohan472000 3559a55
Update evaluate_postfix_notations.py
rohan472000 7f6a692
Update data_structures/stacks/evaluate_postfix_notations.py
rohan472000 748eff5
Update data_structures/stacks/evaluate_postfix_notations.py
rohan472000 816120b
Update data_structures/stacks/evaluate_postfix_notations.py
rohan472000 9e6f36a
Update data_structures/stacks/evaluate_postfix_notations.py
rohan472000 2779cd0
Update evaluate_postfix_notations.py
rohan472000 2bbac37
Update evaluate_postfix_notations.py
rohan472000 c4f050e
Update data_structures/stacks/evaluate_postfix_notations.py
rohan472000 c3a7c67
Update data_structures/stacks/evaluate_postfix_notations.py
rohan472000 29b28ec
Update evaluate_postfix_notations.py
rohan472000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,17 +32,23 @@ 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) | ||
if token == "-" and len(stack) < 2: | ||
operand = stack.pop() | ||
stack.append(-operand) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this 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. This block will handle +,*,/ operators as above block(if block) is handling "-" operator.
rohan472000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
if a * b < 0 and a % b != 0: | ||
stack.append(a // b + 1) | ||
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: | ||
stack.append(int(token)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 justlen(stack) < 2
.