Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create postfix_evaluation.py
  • Loading branch information
ashwek authored Nov 30, 2018
commit 92f8271e745e77abf52a564cb957ee94848c9e15
50 changes: 50 additions & 0 deletions data_structures/stacks/postfix_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
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
"""

import operator as op

def Solve(Postfix):
Stack = []
Div = lambda x, y: int(x/y) # integer division operation
Opr = {'^':op.pow, '*':op.mul, '/':Div, '+':op.add, '-':op.sub} # operators & their respective operation

# print table header
print('Symbol'.center(8), 'Action'.center(12), 'Stack', sep = " | ")
print('-'*(30+len(Postfix)))

for x in Postfix:
if( x.isdigit() ): # if x in digit
Stack.append(x) # append x to stack
print(x.rjust(8), ('push('+x+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format
else:
B = Stack.pop() # pop stack
print("".rjust(8), ('pop('+B+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format

A = Stack.pop() # pop stack
print("".rjust(8), ('pop('+A+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format

Stack.append( str(Opr[x](int(A), int(B))) ) # evaluate the 2 values poped from stack & push result to stack
print(x.rjust(8), ('push('+A+x+B+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format

return int(Stack[0])


if __name__ == "__main__":
Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(' ')
print("\n\tResult = ", Solve(Postfix))