Write a python program to implement parenthesis mismatch using stack
In [ ]:
class stack(object):
'''Class to implement stack data structure with the following methods:
-push(element)
-pop()'''
def __init__(self):
self.s = []
def push(self,elem):
self.s.append(elem)
def pop(self):
p = self.s[-1]
del self.s[-1]
return p
def empty(self):
if len(self.s)==0:
return True
else:
return False
def parenMismatch(expression):
'''Function to return True in case of parenthesis mismatch in an expression and False otherwise'''
s = stack()
for i in xrange(len(expression)):
if expression[i]=='(':
s.push('(')
elif expression[i]==')':
s.pop()
if s.empty():
return False
else:
return True