-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathpostfix_evaluation.py
233 lines (208 loc) · 6.77 KB
/
postfix_evaluation.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""
The Reverse Polish Nation also known as Polish postfix notation
or simply postfix notation.
https://en.wikipedia.org/wiki/Reverse_Polish_notation
Classic examples of simple stack implementations
Valid operators are +, -, *, /.
Each operand may be an integer or another expression.
Output:
Enter a Postfix Equation (space separated) = 5 6 9 * +
Symbol | Action | Stack
-----------------------------------
5 | push(5) | 5
6 | push(6) | 5,6
9 | push(9) | 5,6,9
| pop(9) | 5,6
| pop(6) | 5
* | push(6*9) | 5,54
| pop(54) | 5
| pop(5) |
+ | push(5+54) | 59
Result = 59
"""
# Defining valid unary operator symbols
UNARY_OP_SYMBOLS = ("-", "+")
# Defining valid binary operator symbols
BINARY_OP_SYMBOLS = ("-", "+", "*", "^", "/")
def parse_token(data: str) -> float | str:
"""
Converts the given data to appropriate number if it is indeed a number, else returns
the data as it is with a False flag. This function also serves as a check of whether
the input is a number or not.
Parameters
----------
data : str
The data which needs to be converted to the appropriate number
Returns
-------
bool, int or float
Returns a tuple of (a, b) where 'a' is True if data is indeed a number (integer
or numeric) and 'b' is either an integer of a floating point number.
If 'a' is False, then b is 'data'
"""
try:
return int(data)
except ValueError:
try:
return float(data)
except ValueError:
if is_operator(data):
return data
msg = f"{data} is neither a number nor a valid operator"
raise ValueError(msg)
def is_operator(token: str | float) -> bool:
"""
Checks whether a given input is one of the valid operators or not.
Valid operators being '-', '+', '*', '^' and '/'.
Parameters
----------
token : str
The value that needs to be checked for operator
Returns
-------
bool
True if data is an operator else False.
"""
return token in BINARY_OP_SYMBOLS
def evaluate(post_fix: list[str], verbose: bool = False) -> float:
"""
Function that evaluates postfix expression using a stack.
>>> evaluate(["2", "1", "+", "3", "*"])
9.0
>>> evaluate(["4", "13", "5", "/", "+"])
6.6
>>> evaluate(["2", "-", "3", "+"])
1.0
>>> evaluate(["-4", "5", "*", "6", "-"])
-26.0
>>> evaluate([])
0
>>> evaluate(["4", "-", "6", "7", "/", "9", "8"])
Traceback (most recent call last):
...
ArithmeticError: Input is not a valid postfix expression
Parameters
----------
post_fix : list
The postfix expression tokenized into operators and operands and stored as a
python list
verbose : bool
Display stack contents while evaluating the expression if verbose is True
Returns
-------
float
The evaluated value
"""
stack = []
valid_expression = []
opr = {
"^": lambda p, q: p**q,
"*": lambda p, q: p * q,
"/": lambda p, q: p / q,
"+": lambda p, q: p + q,
"-": lambda p, q: p - q,
} # operators & their respective operation
if len(post_fix) == 0:
return 0
# Checking the list to find out whether the postfix expression is valid
valid_expression = [parse_token(token) for token in post_fix]
if verbose:
# print table header
print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ")
print("-" * (30 + len(post_fix)))
for x in valid_expression:
if not is_operator(x):
stack.append(x) # append x to stack
if verbose:
# output in tabular format
print(
str(x).rjust(8),
("push(" + str(x) + ")").ljust(12),
stack,
sep=" | ",
)
continue
# If x is operator
# If only 1 value is inside stack and + or - is encountered
# then this is unary + or - case
if x in UNARY_OP_SYMBOLS and len(stack) < 2:
b = stack.pop() # pop stack
if x == "-":
b *= -1 # negate b
stack.append(b)
if verbose:
# output in tabular format
print(
"".rjust(8),
("pop(" + str(b) + ")").ljust(12),
stack,
sep=" | ",
)
print(
str(x).rjust(8),
("push(" + str(x) + str(b) + ")").ljust(12),
stack,
sep=" | ",
)
continue
b = stack.pop() # pop stack
if verbose:
# output in tabular format
print(
"".rjust(8),
("pop(" + str(b) + ")").ljust(12),
stack,
sep=" | ",
)
a = stack.pop() # pop stack
if verbose:
# output in tabular format
print(
"".rjust(8),
("pop(" + str(a) + ")").ljust(12),
stack,
sep=" | ",
)
# evaluate the 2 values popped from stack & push result to stack
stack.append(opr[str(x)](a, b))
if verbose:
# output in tabular format
print(
str(x).rjust(8),
("push(" + str(a) + str(x) + str(b) + ")").ljust(12),
stack,
sep=" | ",
)
# If everything executed correctly, the stack will contain
# only one element which is the result
if len(stack) != 1:
raise ArithmeticError("Input is not a valid postfix expression")
return float(stack[0])
def is_yes(val: str) -> bool:
"""
Function that checks whether a user has entered any representation of a Yes (y, Y).
Any other input is considered as a No.
Parameters
-----------
val : str
The value entered by user
Returns
-------
bool
True if Yes, otherwise False
"""
return val in ("Y", "y")
if __name__ == "__main__":
loop = True
# Creating a loop so that user can evaluate postfix expression multiple times
while True:
expression = input("Enter a Postfix Expression (space separated): ").split(" ")
choice = input(
"Do you want to see stack contents while evaluating? [y/N]: "
).strip()
display = is_yes(choice)
output = evaluate(expression, display)
print("Result = ", output)
choice = input("Do you want to enter another expression? [y/N]: ")
if not is_yes(choice):
break