Exemple #1
0
def check_parens(text):
    """
    定义括号匹配函数
    """
    st = ListStack()    # 实例化栈

    for per,i in parentheses(text):
        if per in open_parens:
            st.push(per)
        elif st.pop() != match_parens_dict[per]:
            print('不匹配的括号是:', i, per)
            return False

    print('匹配成功')
    return True
Exemple #2
0
def postorder_non_recursion(t, proc):
    s = ListStack()
    while t is not None or not s.is_empty():
        while t is not None:
            s.push(t)
            t = t.left if t.left is not None else t.right
        t = s.pop()
        proc(t.data)
        if not s.is_empty() and s.top().left == t:
            t = s.top().right
        else:
            t = None
    def preorder_elements(self):
        t, s = self._root, ListStack()
        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()
Exemple #4
0
 def test_push_two_then_pop(self):
     stack = ListStack()
     stack.push(1)
     stack.push(2)
     res = stack.pop()
     self.assertEqual(res, 2)
     self.assertEqual(stack.list(), [1])
def find_path_backtrace(maze, start, end):
    if start == end:  # 如果入口就是出口,直接找到,搜索结束,打印出来就行
        print(start)
        return

    st = ListStack()  # 实例化栈
    mark(maze, start)  # 压入栈的时候先标记他,这样就保证了任何位置不会被压入两次,避免重复
    st.push((start, 0))  # 入口和方向0的序对入栈

    while not st.is_empty():  # 回退
        pos, nxt = st.pop()  # 取栈顶以及他的探查方向
        for i in range(nxt, 4):
            nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1])  # 得到下一位置
            if nextp == end:  # 到达出口,打印路径
                print_path(end, pos, st)
                return
            if possible(maze, nextp):  # 遇到未探查的新位置
                st.push((pos, i + 1))  # 原位置和下一方向入栈
                mark(maze, nextp)
                st.push((nextp, 0))  # 新位置入栈
                break  # 退出内层循环,下次迭代将以新栈顶未当前位置继续
    print('对不起!找不到路径...')  # 找不到路径
Exemple #6
0
def preorder_non_recursion(t, proc):
    s = ListStack()
    while t is not None or not s.is_empty():
        while t is not None:
            proc(t.data)
            s.push(t.right)
            t = t.left
        t = s.pop()
Exemple #7
0
def preorder_non_recursion_elements(t):
    s = ListStack()
    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()
Exemple #8
0
 def test_is_empty_when_pushed_and_deleted(self):
     stack = ListStack()
     stack.push(1)
     stack.pop()
     self.assertTrue(stack.isEmpty())
Exemple #9
0
 def test_is_empty_when_pushed(self):
     stack = ListStack()
     stack.push(1)
     self.assertFalse(stack.isEmpty())
Exemple #10
0
 def test_push_two(self):
     stack = ListStack()
     stack.push(1)
     stack.push(2)
     self.assertEqual(stack.list(), [2, 1])
     self.assertEqual(stack.peek(), 2)
Exemple #11
0
 def test_is_empty_when_empty(self):
     stack = ListStack()
     self.assertTrue(stack.isEmpty())
Exemple #12
0
 def test_pop_empty(self):
     stack = ListStack()
     with self.assertRaises(ValueError):
         res = stack.pop()
Exemple #13
0
 def test_push_until_full(self):
     stack = ListStack(5)
     for i in range(5):
         stack.push(i)
     self.assertEqual(stack.list(), [4, 3, 2, 1, 0])
Exemple #14
0
 def test_peek_non_empty(self):
     stack = ListStack()
     stack.push(1)
     self.assertEqual(stack.peek(), 1)
Exemple #15
0
 def test_peek_empty(self):
     stack = ListStack()
     with self.assertRaises(ValueError):
         stack.peek()
Exemple #16
0
 def test_push_one(self):
     stack = ListStack()
     stack.push(1)
     self.assertEqual(stack.list(), [1])
     self.assertEqual(stack.peek(), 1)
def trans_infix_suffix(line):
    st = ListStack()
    exp = []

    for x in tokens(line):  # 循环便利处理好了的表达式
        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():  # 最后将栈中剩下的运算符依次放入表达式
        exp.append(st.pop())

    return exp