2019-03-01 16:53:29 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2019-11-16 07:05:00 +00:00
|
|
|
Result = 59
|
2019-03-01 16:53:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import operator as op
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def solve(post_fix):
|
|
|
|
stack = []
|
|
|
|
div = lambda x, y: int(x / y) # noqa: E731 integer division operation
|
|
|
|
opr = {
|
2019-10-05 05:14:13 +00:00
|
|
|
"^": op.pow,
|
|
|
|
"*": op.mul,
|
2022-10-12 22:54:20 +00:00
|
|
|
"/": div,
|
2019-10-05 05:14:13 +00:00
|
|
|
"+": op.add,
|
|
|
|
"-": op.sub,
|
|
|
|
} # operators & their respective operation
|
2019-03-01 16:53:29 +00:00
|
|
|
|
|
|
|
# print table header
|
2019-10-05 05:14:13 +00:00
|
|
|
print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ")
|
2022-10-12 22:54:20 +00:00
|
|
|
print("-" * (30 + len(post_fix)))
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
for x in post_fix:
|
2019-10-05 05:14:13 +00:00
|
|
|
if x.isdigit(): # if x in digit
|
2022-10-12 22:54:20 +00:00
|
|
|
stack.append(x) # append x to stack
|
2020-05-22 06:10:11 +00:00
|
|
|
# output in tabular format
|
2022-10-12 22:54:20 +00:00
|
|
|
print(x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(stack), sep=" | ")
|
2019-03-01 16:53:29 +00:00
|
|
|
else:
|
2022-10-12 22:54:20 +00:00
|
|
|
b = stack.pop() # pop stack
|
2020-05-22 06:10:11 +00:00
|
|
|
# output in tabular format
|
2022-10-12 22:54:20 +00:00
|
|
|
print("".rjust(8), ("pop(" + b + ")").ljust(12), ",".join(stack), sep=" | ")
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
a = stack.pop() # pop stack
|
2020-05-22 06:10:11 +00:00
|
|
|
# output in tabular format
|
2022-10-12 22:54:20 +00:00
|
|
|
print("".rjust(8), ("pop(" + a + ")").ljust(12), ",".join(stack), sep=" | ")
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
stack.append(
|
|
|
|
str(opr[x](int(a), int(b)))
|
2020-01-18 12:24:33 +00:00
|
|
|
) # evaluate the 2 values popped from stack & push result to stack
|
2020-05-22 06:10:11 +00:00
|
|
|
# output in tabular format
|
2019-10-05 05:14:13 +00:00
|
|
|
print(
|
|
|
|
x.rjust(8),
|
2022-10-12 22:54:20 +00:00
|
|
|
("push(" + a + x + b + ")").ljust(12),
|
|
|
|
",".join(stack),
|
2019-10-05 05:14:13 +00:00
|
|
|
sep=" | ",
|
2020-05-22 06:10:11 +00:00
|
|
|
)
|
2019-03-01 16:53:29 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
return int(stack[0])
|
2019-03-01 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-10-05 05:14:13 +00:00
|
|
|
Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(" ")
|
2022-10-12 22:54:20 +00:00
|
|
|
print("\n\tResult = ", solve(Postfix))
|