-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Adding in the evaluate postfix notation using Stack #2598
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c848c3c
Create evaluate_postfix_notations.py
akashgkrishnan 0ac6611
Create evaluate_postfix_notations.py
akashgkrishnan 73f61b2
Delete evaluate_postfix_notations.py
akashgkrishnan e9334a1
Evaluate postfix expression stack clean approach
akashgkrishnan 1351128
Update evaluate_postfix_notations.py
akashgkrishnan 7e1eb21
Update evaluate_postfix_notations.py
akashgkrishnan e1c15e9
Update evaluate_postfix_notations.py
akashgkrishnan 9e0b8cb
Update evaluate_postfix_notations.py
akashgkrishnan 3668d56
Update evaluate_postfix_notations.py
akashgkrishnan 2f63309
Update evaluate_postfix_notations.py
akashgkrishnan 29530ed
Update data_structures/stacks/evaluate_postfix_notations.py
akashgkrishnan 22393cf
Update data_structures/stacks/evaluate_postfix_notations.py
akashgkrishnan 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# The Reverse Polish Nation also known as Polish postfix notation or simply postfix notation | ||
# https://en.wikipedia.org/wiki/Reverse_Polish_notation | ||
# Classic examples of simple stack implementations | ||
# Valid operators are +, -, *, /. Each operand may be an integer or another expression. | ||
|
||
|
||
def evaluate_postfix(postfix_notation): | ||
akashgkrishnan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> array = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] | ||
>>> evaluate_postfix(array) | ||
22 | ||
>>> array = ["2", "1", "+", "3", "*"] | ||
>>> evaluate_postfix(array) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
9 | ||
>>> array = ["4", "13", "5", "/", "+"] | ||
>>> evaluate_postfix(array) | ||
6 | ||
>>> array = [] | ||
>>> evaluate_postfix(array) | ||
akashgkrishnan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
0 | ||
""" | ||
operations = {'+', '-', '*', '/'} | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stack = [] | ||
|
||
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) | ||
else: | ||
stack.append(a // b) | ||
else: | ||
stack.append(int(token)) | ||
|
||
return stack.pop() if len(stack) != 0 else 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.
Uh oh!
There was an error while loading. Please reload this page.