Skip to content

fixes #8673; Add operator's associativity check for stacks/infix_to_p… #8674

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
14 changes: 7 additions & 7 deletions data_structures/stacks/infix_to_postfix_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ def infix_to_postfix(expression_str: str) -> str:
if char_precedence > tos_precedence:
stack.push(char)
break
elif char_precedence == tos_precedence:
if associativity(char) == "RL":
stack.push(char)
break
else:
postfix.append(stack.pop())
else:
if char_precedence < tos_precedence:
postfix.append(stack.pop())
break
# Precedences are equal
if associativity(char) == "RL":
stack.push(char)
break
postfix.append(stack.pop())

while not stack.is_empty():
postfix.append(stack.pop())
Expand Down