Skip to content

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
wants to merge 16 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions data_structures/stacks/evaluate_postfix_notations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,52 @@ 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
"""
if not postfix_notation:
return 0

operations = {"+", "-", "*", "/"}
unary_operations = {"+"} # Unary operator(s)
Copy link
Contributor

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.

Copy link
Contributor Author

@rohan472000 rohan472000 Jun 11, 2023

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.


stack: list[Any] = []

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 == "-":
if len(stack) < 1:
raise ValueError(
"Invalid expression: insufficient operands for unary operator"
)
Copy link
Contributor

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.

Copy link
Contributor Author

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.

operand = stack.pop()
stack.append(-operand)
else:
if a * b < 0 and a % b != 0:
stack.append(a // b + 1)
else:
stack.append(a // b)
if len(stack) < 2:
raise ValueError(
"Invalid expression: insufficient operands for binary operator"
)
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)
else:
stack.append(int(token))

if len(stack) != 1:
raise ValueError("Invalid expression: insufficient operators")

return stack.pop()


Expand Down