示例#1
0
 def inicializar(self):
     contador = 0
     while contador < 4:
         self.celulas = []
         for i in range(self.linhas):
             linha = []
             for j in range(self.colunas):
                 celula = Celula()
                 celula.y = i
                 celula.x = j
                 celula.inicio = False
                 celula.fim = False
                 celula.visitada = False
                 linha.append(celula)
             self.celulas.append(linha)
         self.celulas[0][0].inicio = True
         self.celulas[-1][-1].fim = True
         contador = contador + 1
         self.pilha = Stack()
         self._qtdVisitadas = 0
         self.criar()
         if (not self.fechada(self.celulas[1][1])) and \
             (not self.fechada(self.celulas[-2][-2])):
             break
     if contador < 4:
         self.valido = True
示例#2
0
 def infix_to_suffix(exp):
     Tree.output = []
     theStack = Stack()
     if not exp:
         return []
     infix = exp.split(" ")
     length = len(infix)
     for j in range(length):
         item = infix[j]
         flag = {
             '+': 1,
             '-': 1,
             'x': 2,
             '÷': 2,
             '(': 3,
             ')': 4
         }.get(item, 5)
         if flag == 1:
             Tree.get_oper(item, 1, theStack)
         elif flag == 2:
             Tree.get_oper(item, 2, theStack)
         elif flag == 3:
             theStack.push(item)
         elif flag == 4:
             Tree.got_paren(theStack)
         elif flag == 5:
             Tree.output.append(item)
     while not theStack.is_empty():
         Tree.output.append(theStack.pop())
     return Tree.output
示例#3
0
 def __init__(self, board):
     self.board = board
     self.moves = KnightsTourMoves()
     self.tour_steps_stack = Stack(max_size=board.size_horizontal *
                                   board.size_vertical)
     self.tries = 0
     self.start_time = time.time()
示例#4
0
 def __init__(self):
     self._pilha = Stack()
     self._corrente = None
     self._caminho = []
     # Norte, Sul, Leste, Oeste:
     self._incrementos = [[0, -1], [0, 1], [1, 0], [-1, 0]]
     self._visitadas = []
     self._plabirinto = None
示例#5
0
文件: answer.py 项目: 1726451295/calc
 def answer_two(expression):
     an = Answer()
     stack = Stack()
     result = Fraction(0, 1)
     # testsuff = "1 1 chu 0 chu 1'1/5 chu 2 +".split(" ")
     suffix = Tree.infix_to_suffix(expression.get_expression())
     str = " ".join(suffix)
     # print(str)
     if not suffix:
         return
     is_num = None
     for item in suffix:
         try:
             is_num = True
             result = Answer.get_fraction(item)
         except Exception:
             is_num = False
         if is_num:
             stack.push(result)
         else:
             flag = {
                 '+': 1,
                 '-': 2,
                 'x': 3,
                 '÷': 4,
             }.get(item, 5)
             if flag == 1:
                 a = stack.pop()
                 b = stack.pop()
                 stack.push(a + b)
             elif flag == 2:
                 a = stack.pop()
                 b = stack.pop()
                 stack.push(b - a)
             elif flag == 3:
                 a = stack.pop()
                 b = stack.pop()
                 stack.push(a * b)
             elif flag == 4:
                 a = stack.pop()
                 b = stack.pop()
                 if a == 0:
                     print("you 0")
                     an.wronum = 1
                     break
                 stack.push(b / a)
             elif flag == 5:
                 pass
     expression.set_fraction(stack.peek())
     expression.set_value(stack.peek())
     return an.wronum
示例#6
0
 def __init__(self, linhas=10, colunas=10):
     self.linhas = linhas
     self.colunas = colunas
     self.celulas = []
     self.celulaInicial = None
     self.celunaFinal = None
     self.valido = False
     self._corrente = None
     self._proxima = None
     self._qtdTotal = 0
     self._qtdVisitadas = 0
     self._pilha = Stack()
     self.inicializar()
     self.caminho = None
示例#7
0
    def create_stack(self):
        screen = True

        while screen:
            button, values = self.__stack_view.create()
            if button == "Cancel":
                screen = False
            elif values[0] == "":
                self.__stack_view.show_message("type_value")

            elif button == "Create":
                stack_length = int(values[0])
                if stack_length <= 0:
                    self.__stack_view.show_message("number_zero")
                else:
                    self.__stack_number += 1
                    stack = Stack(stack_length, self.__stack_number)
                    self.__stacks.append(stack)
                    screen = False
            self.__stack_view.close()
示例#8
0
    def construct(post):
        """构造二叉树"""
        stack = Stack()
        # parent = None
        # right = None
        # left = None
        for item in post:
            if not Tree.is_operator(item):
                parent = Node(item)
                stack.push(parent)
            else:
                parent = Node(item)
                right = stack.pop()
                left = stack.pop()
                parent.right = right
                parent.left = left
                stack.push(parent)

        parent = stack.peek()
        stack.pop()
        return Tree(parent)