# 栈的应用一:简单括号匹配 defparChecker1(symbolString): s = Stack() balanced = True index = 0
while index < len(symbolString) and balanced: symbol = symbolString(index) if symbol == '(' : #遇到左括号就入栈 s.push(symbol) else: if s.isEmpty(): balanced = False else: s.pop() #匹配到右括号就将栈里的左括号出栈 index = index + 1
if balanced and s.isEmpty(): returnTrue else: returnFalse
defparChecker2(symbolString): s = Stack() balanced = True index = 0
while index < len(symbolString) and balanced: symbol = symbolString(index) if symbol in"({[": s.push(symbol) else: if s.isEmpty(): balanced = False else: top = s.pop() ifnot matches(top,symbol): balanced = False index = index + 1
if balanced and s.isEmpty(): returnTrue else: returnFalse