-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix_to_postfix.py
59 lines (49 loc) · 1.98 KB
/
infix_to_postfix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Given an infix expression in the form of string str. Convert this infix expression to postfix expression.
Infix expression: The expression of the form a op b. When an operator is in-between every pair of operands.
Postfix expression: The expression of the form a b op. When an operator is followed for every pair of operands.
Note: The order of precedence is: ^ greater than * equals to / greater than + equals to -. Ignore the right associativity of ^.
Example 1:
Input: str = "a+b*(c^d-e)^(f+g*h)-i"
Output: abcd^e-fgh*+^*+i-
Explanation:
After converting the infix expression
into postfix expression, the resultant
expression will be abcd^e-fgh*+^*+i-
Example 2:
Input: str = "A*(B+C)/D"
Output: ABC+*D/
Explanation:
After converting the infix expression
into postfix expression, the resultant
expression will be ABC+*D/
Your Task:
This is a function problem. You only need to complete the function infixToPostfix() that takes a string(Infix Expression) as a parameter and returns a string(postfix expression). The printing is done automatically by the driver code.
Expected Time Complexity: O(|str|).
Expected Auxiliary Space: O(|str|).
Constraints:
1 ≤ |str| ≤ 105
"""
class Solution:
#Function to convert an infix expression to a postfix expression.
def InfixtoPostfix(self, exp):
#code here
precedence = {'^': 3, '*': 2, '/': 2, '+': 1, '-': 1}
stack = []
postfix = ""
for char in exp:
if char.isalnum():
postfix += char
elif char == '(':
stack.append(char)
elif char == ')':
while stack and stack[-1] != '(':
postfix += stack.pop()
stack.pop() # Remove '('
else:
while stack and precedence[char] <= precedence.get(stack[-1], 0):
postfix += stack.pop()
stack.append(char)
while stack:
postfix += stack.pop()
return postfix