-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert Infix To Prefix Notation.py
91 lines (64 loc) · 2.07 KB
/
Convert Infix To Prefix Notation.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""nput: A * B + C / D
Output: + * A B/ C D
Input: (A – B/C) * (A/K-L)
Output: *-A/BC-/AKL"""
def isOperator(c):
return (not c.isalpha()) and (not c.isdigit())
# Function to get the priority of operators
def getPriority(c):
if c == '-' or c == '+':
return 1
elif c == '*' or c == '/':
return 2
elif c == '^':
return 3
return 0
# Function to convert the infix expression to postfix
def infixToPostfix(infix):
infix = '(' + infix + ')'
l = len(infix)
char_stack = []
output = ""
for i in range(l):
# Check if the character is alphabet or digit
if infix[i].isalpha() or infix[i].isdigit():
output += infix[i]
# If the character is '(' push it in the stack
elif infix[i] == '(':
char_stack.append(infix[i])
# If the character is ')' pop from the stack
elif infix[i] == ')':
while char_stack[-1] != '(':
output += char_stack.pop()
char_stack.pop()
# Found an operator
else:
if isOperator(char_stack[-1]):
if infix[i] == '^':
while getPriority(infix[i]) <= getPriority(char_stack[-1]):
output += char_stack.pop()
else:
while getPriority(infix[i]) < getPriority(char_stack[-1]):
output += char_stack.pop()
char_stack.append(infix[i])
while len(char_stack) != 0:
output += char_stack.pop()
return output
# Function to convert infix expression to prefix
def infixToPrefix(infix):
l = len(infix)
infix = infix[::-1]
for i in range(l):
if infix[i] == '(':
infix[i] = ')'
elif infix[i] == ')':
infix[i] = '('
prefix = infixToPostfix(infix)
prefix = prefix[::-1]
return prefix
# Driver code
if __name__ == '__main__':
s = "x+y*z/w+u"
# Function call
print(infixToPrefix(s))
#code from geeksforgeeks