def check_pares(text): pares = "()[]{}" open_pares = "([{" opposite = {")": "(", "]": "[", "}": "{"} # 表示配对关系的字典 def paretheses(text): # 括号生成器,定义见后 i, text_len = 0, len(text) while True: while i < text_len and text[i] not in pares: i += 1 if i >= text_len: return yield text[i], i i += 1 st = SStack() for pr, i in paretheses(text): # 对 text 里各括号和位置迭代 if pr in open_pares: # 开括号,压进栈并继续 st.push(pr) elif st.pop() != opposite[pr]: # 不匹配就是失败,退出 print("Unmatching is found at", i, "for", pr) return False # else 是一次括号配对成功,什么也不做,继续 print("All paretheses are correctly matched.") return True
def preorder_iter(t): s = SStack() while t is not None or not s.is_empty(): while t is not None: s.push(t.right) yield t.data t = t.left t = s.pop()
def preorder_nonrec(t, proc): s = SStack() while t is not None or not s.is_empty(): while t is not None: # go down along left chain s.push(t.right) # push right branch into stack proc(t.data) t = t.left t = s.pop() # left chain ends, backtrack
def inorder_nonrec(t, proc): s = SStack() while t is not None or not s.is_empty(): while t is not None: s.push(t) t = t.left t = s.pop() proc(t.data) t = t.right
def postorder_nonrec(t, proc): s = SStack() while t is not None or not s.is_empty(): while t is not None: # iterate until top has no child s.push(t) t = t.left if t.left is not None else t.right # if we can go left, go, otherwise, go right t = s.pop() # get the node to be access proc(t.data) if not s.is_empty() and s.top().left == t: t = s.top().right # end of left visit, turn right else: t = None # end of right visit, force to backtrack
def maze_solver1(maze, start, end): st = SStack() pos, nxt = start, 0 while True: if pos == end: print_path1(pos, st) return mark(maze, pos) for i in range(nxt, 4): # 依次检查潜在探索方向 nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1]) # 算出下一点 if passable(maze, nextp): # 遇到未探查点 st.push((pos, i + 1)) # 原位置进栈 pos, nxt = nextp, 0 break else: if st.is_empty(): break pos, nxt = st.pop() print("No path found.") # 找不到路径
def trans_infix_suffix(line): st = SStack() llen = len(line) exp = [] for x in tokens(line): # tokens 是一个待定义的生成器 if x not in infix_operators: # 运算对象直接送出 exp.append(x) elif st.is_empty() or x == '(': # 左括号进栈 st.push(x) elif x == ')': # 处理右括号的分支 while not st.is_empty() and st.top() != "(": exp.append(st.pop()) if st.is_empty(): # 没找到左括号,就是不配对 raise SyntaxError("Missing \'(\'.") st.pop() # 弹出左括号,右括号也不进栈 else: # 处理算术运算符,运算符都看作是左结合 while (not st.is_empty() and priority[st.top()] >= priority[x]): exp.append(st.pop()) st.push(x) # 算术运算符进栈 while not st.is_empty(): # 送出栈里剩下的运算符 if st.top() == "(": # 如果还有左括号,就是不配对 raise SyntaxError("Extra \'(\' in expression.") exp.append(st.pop()) return exp
def maze_solver(maze, start, end): if start == end: print(start) return st = SStack() mark(maze, start) st.push((start, 0)) # start position into stack 入口和方向 0 的序对入栈 while not st.is_empty(): # have possibility to try 走不通时回退 pos, nxt = st.pop() # get last branch position 取栈顶及其探查方向 for i in range(nxt, 4): # try to find unexploring dir(s) 依次检查未探查方向 nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1] ) # next point 算出下一点 if nextp == end: # find end, great! :-) 到达出口,打印路径 print_path(end, pos, st) return if passable(maze, nextp): # new position is passable 遇到未探查的新位置 st.push((pos, i + 1)) # original position in stack 原位置和下一方向入栈 mark(maze, nextp) st.push((nextp, 0)) # new position into stack 新位置入栈 break # 退出内层循环,下次迭代将以新栈顶为当前位置继续 print("No path.") # :-( 找不到路径