使用栈进行复杂符号匹配

from pythonds.basic.stack import Stack
def parchecker(SymbolString):
s=Stack()
balance=True
index=0
while index<len(SymbolString) and balance:
if SymbolString[index] in '{[(':
s.push(SymbolString[index])
elif s.isEmpty():
balance=False
else:
top=s.pop()
if not match(top,SymbolString[index]):
balance=False
if s.isEmpty() and balance:
return True
else:
return False
def match(open,close):
a='{[('
b='}])'
if a.index(open)==b.index(close):
return True
else:
return False
print(parchecker('{{([][])}()}'))

posted on 2018-09-17 22:37  master~hu  阅读(344)  评论(0)    收藏  举报