15
15
"""
16
16
17
17
18
- def infix_2_postfix (infix ) :
18
+ def infix_2_postfix (infix : str ) -> str :
19
19
"""
20
20
>>> infix_2_postfix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
21
21
Symbol | Stack | Postfix
@@ -28,22 +28,35 @@ def infix_2_postfix(infix):
28
28
| + | abc^
29
29
| | abc^+
30
30
'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
+
35
50
>>> infix_2_postfix("")
36
51
Symbol | Stack | Postfix
37
52
----------------------------
38
53
''
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
+
47
60
>>> infix_2_postfix("())")
48
61
Traceback (most recent call last):
49
62
...
@@ -59,7 +72,7 @@ def infix_2_postfix(infix):
59
72
"+" : 1 ,
60
73
"-" : 1 ,
61
74
} # Priority of each operator
62
- print_width = len ( infix ) if ( len (infix ) > 7 ) else 7
75
+ print_width = max ( len (infix ), 7 )
63
76
64
77
# Print table header for output
65
78
print (
@@ -76,14 +89,17 @@ def infix_2_postfix(infix):
76
89
elif x == "(" :
77
90
stack .append (x ) # if x is "(" push to Stack
78
91
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
+
79
95
while stack [- 1 ] != "(" :
80
96
post_fix .append (stack .pop ()) # Pop stack & add the content to Postfix
81
97
stack .pop ()
82
98
else :
83
99
if len (stack ) == 0 :
84
100
stack .append (x ) # If stack is empty, push x to stack
85
101
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 ]]:
87
103
post_fix .append (stack .pop ()) # pop stack & add to Postfix
88
104
stack .append (x ) # push x to stack
89
105
@@ -95,6 +111,9 @@ def infix_2_postfix(infix):
95
111
) # Output in tabular format
96
112
97
113
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
+
98
117
post_fix .append (stack .pop ()) # pop stack & add to Postfix
99
118
print (
100
119
" " .center (8 ),
@@ -106,7 +125,7 @@ def infix_2_postfix(infix):
106
125
return "" .join (post_fix ) # return Postfix as str
107
126
108
127
109
- def infix_2_prefix (infix ) :
128
+ def infix_2_prefix (infix : str ) -> str :
110
129
"""
111
130
>>> infix_2_prefix("a+b^c") # doctest: +NORMALIZE_WHITESPACE
112
131
Symbol | Stack | Postfix
@@ -119,10 +138,23 @@ def infix_2_prefix(infix):
119
138
| | cb^a+
120
139
'+a^bc'
121
140
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'
126
158
127
159
>>> infix_2_prefix('')
128
160
Symbol | Stack | Postfix
@@ -134,26 +166,21 @@ def infix_2_prefix(infix):
134
166
...
135
167
IndexError: list index out of range
136
168
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
145
173
"""
146
- infix = list (infix [::- 1 ]) # reverse the infix equation
174
+ reversed_infix = list (infix [::- 1 ]) # reverse the infix equation
147
175
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 "("
153
181
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 ]
157
184
158
185
159
186
if __name__ == "__main__" :
0 commit comments