1515"""
1616
1717
18- def infix_2_postfix (infix ) :
18+ def infix_2_postfix (infix : str ) -> str :
1919 """
2020 >>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
2121 Symbol | Stack | Postfix
@@ -28,22 +28,35 @@ def infix_2_postfix(infix):
2828 | + | abc^
2929 | | abc^+
3030 'abc^+'
31- >>> infix_2_postfix("1*((-a)*2+b)")
32- Traceback (most recent call last):
33- ...
34- KeyError: '('
31+
32+ >>> infix_2_postfix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
33+ Symbol | Stack | Postfix
34+ -------------------------------------------
35+ 1 | | 1
36+ * | * | 1
37+ ( | *( | 1
38+ ( | *(( | 1
39+ - | *((- | 1
40+ a | *((- | 1a
41+ ) | *( | 1a-
42+ * | *(* | 1a-
43+ 2 | *(* | 1a-2
44+ + | *(+ | 1a-2*
45+ b | *(+ | 1a-2*b
46+ ) | * | 1a-2*b+
47+ | | 1a-2*b+*
48+ '1a-2*b+*'
49+
3550 >>> infix_2_postfix("")
3651 Symbol | Stack | Postfix
3752 ----------------------------
3853 ''
39- >>> infix_2_postfix("(()") # doctest: +NORMALIZE_WHITESPACE
40- Symbol | Stack | Postfix
41- ----------------------------
42- ( | ( |
43- ( | (( |
44- ) | ( |
45- | | (
46- '('
54+
55+ >>> infix_2_postfix("(()")
56+ Traceback (most recent call last):
57+ ...
58+ ValueError: invalid expression
59+
4760 >>> infix_2_postfix("())")
4861 Traceback (most recent call last):
4962 ...
@@ -59,7 +72,7 @@ def infix_2_postfix(infix):
5972 "+" : 1 ,
6073 "-" : 1 ,
6174 } # Priority of each operator
62- print_width = len ( infix ) if ( len (infix ) > 7 ) else 7
75+ print_width = max ( len (infix ), 7 )
6376
6477 # Print table header for output
6578 print (
@@ -76,14 +89,17 @@ def infix_2_postfix(infix):
7689 elif x == "(" :
7790 stack .append (x ) # if x is "(" push to Stack
7891 elif x == ")" : # if x is ")" pop stack until "(" is encountered
92+ if len (stack ) == 0 : # close bracket without open bracket
93+ raise IndexError ("list index out of range" )
94+
7995 while stack [- 1 ] != "(" :
8096 post_fix .append (stack .pop ()) # Pop stack & add the content to Postfix
8197 stack .pop ()
8298 else :
8399 if len (stack ) == 0 :
84100 stack .append (x ) # If stack is empty, push x to stack
85101 else : # while priority of x is not > priority of element in the stack
86- while len ( stack ) > 0 and priority [x ] <= priority [stack [- 1 ]]:
102+ while stack and stack [ - 1 ] != "(" and priority [x ] <= priority [stack [- 1 ]]:
87103 post_fix .append (stack .pop ()) # pop stack & add to Postfix
88104 stack .append (x ) # push x to stack
89105
@@ -95,6 +111,9 @@ def infix_2_postfix(infix):
95111 ) # Output in tabular format
96112
97113 while len (stack ) > 0 : # while stack is not empty
114+ if stack [- 1 ] == "(" : # open bracket with no close bracket
115+ raise ValueError ("invalid expression" )
116+
98117 post_fix .append (stack .pop ()) # pop stack & add to Postfix
99118 print (
100119 " " .center (8 ),
@@ -106,7 +125,7 @@ def infix_2_postfix(infix):
106125 return "" .join (post_fix ) # return Postfix as str
107126
108127
109- def infix_2_prefix (infix ) :
128+ def infix_2_prefix (infix : str ) -> str :
110129 """
111130 >>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
112131 Symbol | Stack | Postfix
@@ -119,10 +138,23 @@ def infix_2_prefix(infix):
119138 | | cb^a+
120139 '+a^bc'
121140
122- >>> infix_2_prefix("1*((-a)*2+b)")
123- Traceback (most recent call last):
124- ...
125- KeyError: '('
141+ >>> infix_2_prefix("1*((-a)*2+b)") # doctest: +NORMALIZE_WHITESPACE
142+ Symbol | Stack | Postfix
143+ -------------------------------------------
144+ ( | ( |
145+ b | ( | b
146+ + | (+ | b
147+ 2 | (+ | b2
148+ * | (+* | b2
149+ ( | (+*( | b2
150+ a | (+*( | b2a
151+ - | (+*(- | b2a
152+ ) | (+* | b2a-
153+ ) | | b2a-*+
154+ * | * | b2a-*+
155+ 1 | * | b2a-*+1
156+ | | b2a-*+1*
157+ '*1+*-a2b'
126158
127159 >>> infix_2_prefix('')
128160 Symbol | Stack | Postfix
@@ -134,26 +166,21 @@ def infix_2_prefix(infix):
134166 ...
135167 IndexError: list index out of range
136168
137- >>> infix_2_prefix('())') # doctest: +NORMALIZE_WHITESPACE
138- Symbol | Stack | Postfix
139- ----------------------------
140- ( | ( |
141- ( | (( |
142- ) | ( |
143- | | (
144- '('
169+ >>> infix_2_prefix('())')
170+ Traceback (most recent call last):
171+ ...
172+ ValueError: invalid expression
145173 """
146- infix = list (infix [::- 1 ]) # reverse the infix equation
174+ reversed_infix = list (infix [::- 1 ]) # reverse the infix equation
147175
148- for i in range (len (infix )):
149- if infix [i ] == "(" :
150- infix [i ] = ")" # change "(" to ")"
151- elif infix [i ] == ")" :
152- infix [i ] = "(" # change ")" to "("
176+ for i in range (len (reversed_infix )):
177+ if reversed_infix [i ] == "(" :
178+ reversed_infix [i ] = ")" # change "(" to ")"
179+ elif reversed_infix [i ] == ")" :
180+ reversed_infix [i ] = "(" # change ")" to "("
153181
154- return (infix_2_postfix ("" .join (infix )))[
155- ::- 1
156- ] # call infix_2_postfix on Infix, return reverse of Postfix
182+ # call infix_2_postfix on Infix, return reverse of Postfix
183+ return (infix_2_postfix ("" .join (reversed_infix )))[::- 1 ]
157184
158185
159186if __name__ == "__main__" :
0 commit comments