使用栈进行复杂符号匹配

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('{{([][])}()}'))

猜你喜欢

转载自www.cnblogs.com/masterhu/p/9665394.html