Python/data_structures/stacks/infix_to_postfix_conversion.py

60 lines
1.8 KiB
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
import string
from .stack import Stack
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
__author__ = "Omkar Pathak"
2018-10-19 12:48:28 +00:00
def is_operand(char):
return char in string.ascii_letters or char in string.digits
def precedence(char):
"""Return integer value representing an operator's precedence, or
2018-10-19 12:48:28 +00:00
order of operation.
https://en.wikipedia.org/wiki/Order_of_operations
"""
2019-10-05 05:14:13 +00:00
dictionary = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
2018-10-19 12:48:28 +00:00
return dictionary.get(char, -1)
def infix_to_postfix(expression):
"""Convert infix notation to postfix notation using the Shunting-yard
2018-10-19 12:48:28 +00:00
algorithm.
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
https://en.wikipedia.org/wiki/Infix_notation
https://en.wikipedia.org/wiki/Reverse_Polish_notation
"""
stack = Stack(len(expression))
postfix = []
for char in expression:
if is_operand(char):
postfix.append(char)
2019-10-05 05:14:13 +00:00
elif char not in {"(", ")"}:
while not stack.is_empty() and precedence(char) <= precedence(stack.peek()):
2018-10-19 12:48:28 +00:00
postfix.append(stack.pop())
stack.push(char)
2019-10-05 05:14:13 +00:00
elif char == "(":
2018-10-19 12:48:28 +00:00
stack.push(char)
2019-10-05 05:14:13 +00:00
elif char == ")":
while not stack.is_empty() and stack.peek() != "(":
2018-10-19 12:48:28 +00:00
postfix.append(stack.pop())
# Pop '(' from stack. If there is no '(', there is a mismatched
# parentheses.
2019-10-05 05:14:13 +00:00
if stack.peek() != "(":
raise ValueError("Mismatched parentheses")
2018-10-19 12:48:28 +00:00
stack.pop()
while not stack.is_empty():
postfix.append(stack.pop())
2019-10-05 05:14:13 +00:00
return " ".join(postfix)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
expression = "a+b*(c^d-e)^(f+g*h)-i"
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
print("Infix to Postfix Notation demonstration:\n")
print("Infix notation: " + expression)
print("Postfix notation: " + infix_to_postfix(expression))