Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Added Infix to Postfix Conversion in Python #235

Merged
merged 3 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# function to convert infix to postfix using array as stack
def infix_to_postfix(infix_expression):
"""
Function to change infix expression to postfix one
Params:
infix_expression: Infix Expression provided by user
Returns:
postfix_expression: Postfix Expression convertef from Infix one
"""

# initially empty stack
stack = []
# initially empty postfix_expression
postfix_expression = ""
for char in infix_expression:
# if an operand then put it directly in postfix expression
if char not in operators:
postfix_expression += char
# else if operators should be put in stack
elif char == "(":
"""append function to push
elements in the stack"""
stack.append("(")
elif char == ")":
while stack and stack[-1] != "(":
postfix_expression += stack.pop()
""" pop function to pop
elements from stack in LIFO order """
stack.pop()
else:
"""if priority of char in infix_expression is less than or equal to
char at stack[-1] pop out and put in postfix_expression"""
while (
stack and stack[-1] != "(" and priorities[char] <= priorities[stack[-1]]
):
postfix_expression += stack.pop()
stack.append(char)
while stack:
postfix_expression += stack.pop()
return postfix_expression


# Set of operators
operators = set(["+", "-", "*", "/", "(", ")", "^"])

# dictionary having priorities
priorities = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}

print("Input")
infix_expression = input("Enter infix expression\n")
print("Output")

# Displaying the Output
print("Postfix expression: ", infix_to_postfix(infix_expression))
27 changes: 27 additions & 0 deletions Basic-Scripts/Infix_to_Postfix_Conversion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### Infix to Postfix

> • In Infix expression operator is in between every pair of operands.<br>
> • In Postfix expression, the operator is followed for every pair of operands.

> Infix expression is converted to postfix conversion using Stack.
> Postfix expression is evaluated using Stack in Left to Right order.

##### If the scanned character is operand, show it as output. Else, If precedence of scanned operator is greater than the precedence of the operator in the stack,push it.

> Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator.

• If the scanned input is '(', push it into stack<br>
• If the scanned input is ')' ,pop the stack and the output it until '(' comes.<br>
• Repeat above steps. Continue Pop and output from stack until it becomes empty.

##### It makes the code more efficient and even reduces the time complexity.
### Constraints
"""
Input:
Enter infix expression: A+C*(B^D-E)^(G+H*K)-K
Output:
Postfix expression: ACBD^E-GHK*+^*+K-

Time Complexity : O(n)
Space Complexity: Θ(n)
"""