forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfixtoinfix.py
More file actions
43 lines (33 loc) · 764 Bytes
/
Postfixtoinfix.py
File metadata and controls
43 lines (33 loc) · 764 Bytes
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
# Python3 program to find infix for
# a given postfix.
def isOperand(x):
return ((x >= 'a' and x <= 'z') or
(x >= 'A' and x <= 'Z'))
# Get Infix for a given postfix
# expression
def getInfix(exp) :
s = []
for i in exp:
# Push operands
if (isOperand(i)) :
s.insert(0, i)
# We assume that input is a
# valid postfix and expect
# an operator.
else:
op1 = s[0]
s.pop(0)
op2 = s[0]
s.pop(0)
s.insert(0, "(" + op2 + i +
op1 + ")")
# There must be a single element in
# stack now which is the required
# infix.
return s[0]
# Driver Code
if __name__ == '__main__':
exp = "ab*c+"
print(getInfix(exp.strip()))
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)