首页 > 其他 > 详细

使用栈进行复杂符号匹配

时间:2018-09-17 23:46:18      阅读:223      评论:0      收藏:0      [点我收藏+]
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(‘{{([][])}()}‘))

使用栈进行复杂符号匹配

原文:https://www.cnblogs.com/masterhu/p/9665394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!