15
15
"""
16
16
17
17
18
- def infix_2_postfix (infix ) :
18
+ def infix_2_postfix (infix : str ) -> str :
19
19
stack = []
20
20
post_fix = []
21
21
priority = {
@@ -26,6 +26,7 @@ def infix_2_postfix(infix):
26
26
"+" : 1 ,
27
27
"-" : 1 ,
28
28
} # Priority of each operator
29
+
29
30
print_width = len (infix ) if (len (infix ) > 7 ) else 7
30
31
31
32
# Print table header for output
@@ -43,14 +44,18 @@ def infix_2_postfix(infix):
43
44
elif x == "(" :
44
45
stack .append (x ) # if x is "(" push to Stack
45
46
elif x == ")" : # if x is ")" pop stack until "(" is encountered
47
+ if len (stack ) == 0 : # close bracket without open bracket
48
+ raise ValueError ("Invalid bracket position(s)" )
49
+
46
50
while stack [- 1 ] != "(" :
47
51
post_fix .append (stack .pop ()) # Pop stack & add the content to Postfix
48
52
stack .pop ()
49
53
else :
50
54
if len (stack ) == 0 :
51
55
stack .append (x ) # If stack is empty, push x to stack
52
56
else : # while priority of x is not > priority of element in the stack
53
- while len (stack ) > 0 and priority [x ] <= priority [stack [- 1 ]]:
57
+ while len (stack ) > 0 and stack [- 1 ] != '(' \
58
+ and priority [x ] <= priority [stack [- 1 ]]:
54
59
post_fix .append (stack .pop ()) # pop stack & add to Postfix
55
60
stack .append (x ) # push x to stack
56
61
@@ -62,6 +67,9 @@ def infix_2_postfix(infix):
62
67
) # Output in tabular format
63
68
64
69
while len (stack ) > 0 : # while stack is not empty
70
+ if stack [- 1 ] == '(' : # open bracket with no close bracket
71
+ raise ValueError ("Invalid bracket position(s)" )
72
+
65
73
post_fix .append (stack .pop ()) # pop stack & add to Postfix
66
74
print (
67
75
" " .center (8 ),
@@ -73,9 +81,54 @@ def infix_2_postfix(infix):
73
81
return "" .join (post_fix ) # return Postfix as str
74
82
75
83
76
- def infix_2_prefix (infix ):
77
- infix = list (infix [::- 1 ]) # reverse the infix equation
84
+ def infix_2_prefix (infix : str ) -> str :
85
+ """
86
+ >>> infix_2_prefix('a+b^c')
87
+ Symbol | Stack | Postfix
88
+ ----------------------------
89
+ c | | c
90
+ ^ | ^ | c
91
+ b | ^ | cb
92
+ + | + | cb^
93
+ a | + | cb^a
94
+ | | cb^a+
95
+ '+a^bc'
96
+
97
+ >>> infix_2_prefix('1*((-a)*2+b)')
98
+ Symbol | Stack | Postfix
99
+ -------------------------------------------
100
+ ( | ( |
101
+ b | ( | b
102
+ + | (+ | b
103
+ 2 | (+ | b2
104
+ * | (+* | b2
105
+ ( | (+*( | b2
106
+ a | (+*( | b2a
107
+ - | (+*(- | b2a
108
+ ) | (+* | b2a-
109
+ ) | | b2a-*+
110
+ * | * | b2a-*+
111
+ 1 | * | b2a-*+1
112
+ | | b2a-*+1*
113
+ '*1+*-a2b'
114
+
115
+ >>> infix_2_prefix('')
116
+ Symbol | Stack | Postfix
117
+ ----------------------------
118
+ ''
119
+
120
+ >>> infix_2_prefix('(()')
121
+ Traceback (most recent call last):
122
+ ...
123
+ ValueError: Invalid bracket position(s)
78
124
125
+ >>> infix_2_prefix('())')
126
+ Traceback (most recent call last):
127
+ ...
128
+ ValueError: Invalid bracket position(s)
129
+ """
130
+ infix = list (infix [::- 1 ]) # reverse the infix equation
131
+
79
132
for i in range (len (infix )):
80
133
if infix [i ] == "(" :
81
134
infix [i ] = ")" # change "(" to ")"
@@ -88,6 +141,9 @@ def infix_2_prefix(infix):
88
141
89
142
90
143
if __name__ == "__main__" :
144
+ from doctest import testmod
145
+ testmod ()
146
+
91
147
Infix = input ("\n Enter an Infix Equation = " ) # Input an Infix equation
92
148
Infix = "" .join (Infix .split ()) # Remove spaces from the input
93
149
print ("\n \t " , Infix , "(Infix) -> " , infix_2_prefix (Infix ), "(Prefix)" )
0 commit comments