Skip to content

Instantly share code, notes, and snippets.

@Abhayparashar31
Last active September 30, 2020 04:19
Show Gist options
  • Save Abhayparashar31/83e5822f55a74d9c6b8c37939c9559e2 to your computer and use it in GitHub Desktop.
Save Abhayparashar31/83e5822f55a74d9c6b8c37939c9559e2 to your computer and use it in GitHub Desktop.
class Stack():
def __init__(self):
self.items = []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return self.items==[]
def peek(self):
if not self.is_empty():
return self.items[-1]
def show_stack(self):
return self.items
obj = Stack()
def evaluatePostfix(exp):
for i in exp:
# If the scanned character is an operand
# (number here) push it to the stack
if i.isdigit():
obj.push(i)
# If the scanned character is an operator,
# pop two elements from stack and apply it.
else:
val1 = obj.pop()
val2 = obj.pop()
obj.push(str(eval(val2 + i + val1)))
return int(obj.pop())
exp = str(input("Enter the string\n"))
print ("postfix evaluation result: "+ str(evaluatePostfix(exp)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment