def find_path(self): stack = Stack() current = self.startCell current.path.append(current) current.parent = current if current == self.exitCell: return True home_cords_nei = self.check_neighbors(current.row, current.col) for cell in home_cords_nei: if cell == self.exitCell: return True cell.path.append(current) stack.push(cell) self.mark_tried(current.row, current.col) while not stack.isEmpty(): parent = current current = stack.peek() current.parent = parent current.path += parent.path current.path.append(current) self.mark_tried(current.row, current.col) if self.exit_found(current.row, current.col): self.exit = current return True stack.pop() for cell in self.check_neighbors(current.row, current.col): stack.push(cell) return False
def find_path(self): """ Attempts to solve the maze by finding a path from the starting cell to the exit. Returns True if a path is found and False otherwise. """ stack_of_cells = Stack() stack_of_cells.push((self._start_cell.row, self._start_cell.col)) self._mark_path(self._start_cell.row, self._start_cell.col) while not stack_of_cells.is_empty(): current_cell = stack_of_cells.peek() valid_neighbor = False for direction in directions: if self._valid_move(current_cell[0] + direction[0], current_cell[1] + direction[1]): stack_of_cells.push((current_cell[0] + direction[0], current_cell[1] + direction[1])) valid_neighbor = True self._mark_path(current_cell[0] + direction[0], current_cell[1] + direction[1]) if self._exit_found(current_cell[0] + direction[0], current_cell[1] + direction[1]): return True break #we don't need 2 valid neighbor at the same time if not valid_neighbor: self._mark_tried(current_cell[0], current_cell[1]) stack_of_cells.pop() return False
def findPath(self): path = Stack() path.push(self._startCell) self._mazeCells[self._startCell.row, self._startCell.col] = \ self.PATH_TOKEN while not self._exitFound(path.peek().row, path.peek().col): curr = path.peek() if self._validMove(curr.row - 1, curr.col): path.push(_CellPosition(curr.row - 1, curr.col)) self._markPath(curr.row - 1, curr.col) elif self._validMove(curr.row, curr.col - 1): path.push(_CellPosition(curr.row, curr.col - 1)) self._markPath(curr.row, curr.col - 1) elif self._validMove(curr.row + 1, curr.col): path.push(_CellPosition(curr.row + 1, curr.col)) self._markPath(curr.row + 1, curr.col) elif self._validMove(curr.row, curr.col + 1): path.push(_CellPosition(curr.row, curr.col + 1)) self._markPath(curr.row, curr.col + 1) else: path.pop() self._markTried(curr.row, curr.col) if len(path) == 0: return False return True
class ExtoPostfix: """ Receive an infix expression and turn it into a postfix expression. This is a helper module of postfixCal.py. """ def __init__(self): self._isp = {'#':0, '(':1, '*':5, '/':5, '+':3, '-':3, ')':6} #in stack priority self._icp = {'#':0, '(':6, '*':4, '/':4, '+':2, '-':2, ')':1} #in coming priority self._postfix = [] #the list used to save the final postfix expression. self._oprtStack = Stack() #the stack used to change the order of operators. def __str__(self): self._postfix = [elem for elem in self._postfix if elem != ')' and elem != '('] return ','.join(self._postfix) def _getPostfix(self,expr): """ Algorithm: 1)initialize the _oprtStack by pushing '#' into it. 2)read in the first element of the infix expr, and assign it to variable ch. 3)repeat the following steps until ch = '#', meanwhile, the top element of the _oprtStack is also '#': a)if ch is operand, append it to the _postfix, and read the next ch; b)if ch is operator, compare the priority(icp) of ch and the priority(isp) of the top element(op) in _oprtStack: if icp(ch) > isp(op), push ch onto _oprtStack, and read another ch if icp(ch) < isp(op), pop the top element in _oprtStack, and append it to _postfix if icp(ch) == isp(op), pop the top element in _oprtStack,if op is '(', read another ch. """ expr = expr.split() self._oprtStack.push('#') ch = expr.pop(0) while self._oprtStack.isEmpty()==False and ch != '#': if ch.isdigit(): self._postfix.append(ch) ch = expr.pop(0) else: ch1 = self._oprtStack.peek() if self._isp[ch1] < self._icp[ch]: self._oprtStack.push(ch) ch = expr.pop(0) elif self._isp[ch1] > self._icp[ch]: out = self._oprtStack.pop() self._postfix.append(out) else: out = self._oprtStack.pop() if out == '(': ch = expr.pop(0) for i in range(len(self._oprtStack)-1): ch1 = self._oprtStack.pop() self._postfix.append(ch1) def backExpr(self,expr): self._getPostfix(expr) return self._postfix
def findPath( self ): stack = Stack() self._markPath(self._startCell.row, self._startCell.col) stack.push(self._startCell) while not stack.isEmpty(): # debug stack.show() cur = _CellPosition(0, 0) cur.row = stack.peek().row cur.col = stack.peek().col print "current:" print (cur.row, cur.col) if cur.row == self._exitCell.row and \ cur.col == self._exitCell.col: print "findPath ok" return 1 if self.haveUp(cur.row, cur.col): cur.row = cur.row - 1 stack.push(cur) self._markPath(cur.row, cur.col) continue if self.haveRight(cur.row, cur.col): cur.col = cur.col + 1 stack.push(cur) self._markPath(cur.row, cur.col) continue if self.haveDown(cur.row, cur.col): cur.row = cur.row + 1 stack.push(cur) self._markPath(cur.row, cur.col) continue if self.haveLeft(cur.row, cur.col): cur.col = cur.col - 1 stack.push(cur) self._markPath(cur.row, cur.col) continue print "aaaaaaaaaaaaa" self._markTried(cur.row, cur.col) stack.pop() stack.show() print "findPath fail" return 0
def find_path(self): """ Attempts to solve the maze by finding a path from the starting cell to the exit. Returns True if a path is found and False otherwise. """ empty = self.empty_path() self.reset() mid_pos = self._start_cell path = Stack() self._mark_path(mid_pos.row, mid_pos.col) path.push(mid_pos) while mid_pos != self._exit_cell and empty: if _CellPosition(mid_pos.row - 1, mid_pos.col) in empty: mid_pos = _CellPosition(mid_pos.row - 1, mid_pos.col) empty.remove(mid_pos) self._mark_path(mid_pos.row, mid_pos.col) path.push(mid_pos) continue elif _CellPosition(mid_pos.row, mid_pos.col + 1) in empty: mid_pos = _CellPosition(mid_pos.row, mid_pos.col + 1) empty.remove(mid_pos) self._mark_path(mid_pos.row, mid_pos.col) path.push(mid_pos) elif _CellPosition(mid_pos.row + 1, mid_pos.col) in empty: mid_pos = _CellPosition(mid_pos.row + 1, mid_pos.col) empty.remove(mid_pos) self._mark_path(mid_pos.row, mid_pos.col) path.push(mid_pos) elif _CellPosition(mid_pos.row, mid_pos.col - 1) in empty: mid_pos = _CellPosition(mid_pos.row, mid_pos.col - 1) empty.remove(mid_pos) self._mark_path(mid_pos.row, mid_pos.col) path.push(mid_pos) else: self._mark_tried(mid_pos.row, mid_pos.col) path.pop() try: mid_pos = path.peek() except AssertionError: return False return True
def isValidSource(srcFile): """ The function accepts a file object, which we assume was previously opened and contains C++ source code.The file is scanned one line at a time and each line is scanned one character at a time to determine if it contains properly paired and balanced delimiters. """ s = Stack() for line in srcFile: for token in line: if token in "{[(": # if token is one of the opening delimiters it should be pushed # into the stack. s.push(token) # if token is one of the closing delimiters it should be checke by # the following steps. elif token in ")]}": if s.isEmpty(): return False else: left = s.pop() if token == "}" and left != "{" or \ token == "]" and left != "[" or \ token == ")" and left != "(": return False return s.isEmpty()
def findPath( self ): path_stack = Stack() cur_cell = self._startCell path_stack.push(cur_cell) self._markPath( cur_cell.row, cur_cell.col ) while len( path_stack ) != 0: flag_valid = 0 for (v, h) in [(1, 0), (-1, 0), (0, -1), (0 ,1)]: cur_row = cur_cell.row + v cur_col = cur_cell.col + h if self._validMove( cur_row, cur_col ): path_stack.push( _CellPosition(cur_row, cur_col) ) self._markPath( cur_row, cur_col ) #print "( " + str(cur_row) + ", " + str(cur_col) + " ) is valid Path Point" flag_valid = 1 break if flag_valid == 0: pop_cell = path_stack.pop() self._markTried( pop_cell.row, pop_cell.col ) #print "( " + str(pop_cell.row) + ", " + str(pop_cell.col) + " ) is Invalid Path Point" if len(path_stack) == 0: return False cur_cell = path_stack.peek() #print "( " + str(cur_cell.row) + ", " + str(cur_cell.col) + " ) is on the top." if self._exitFound( cur_cell.row, cur_cell.col ): #print len(path_stack) return True
class _BSTMapIterator: """ Iterator for the binary search tree using a software stack """ def __init__(self, root): """Create a stack for use in traversing the tree. """ self._theStack = Stack() self._traverseToMinNode(root) def __iter__(self): return self def next(self): """Returns the next item from the BST in key order""" # If the stack is empty, we are done. if self._theStack.isEmpty(): raise StopIteration else: # The top node on the stack contains the next key. node = self._theStack.pop() key = node.key # If this node has a subtree rooted as the right child, we must # find the node in that subtree that contains the smallest key. # Again, the nodes along the path are pushed on the stack. if node.right is not None: self._traverseToMinNode(node.right) return key def _traverseToMinNode(self, subtree): """ Traverses down the subtree to find the node containing the smallest key during which the nodes along that path are pushed onto the stack.""" if subtree is not None: self._theStack.push(subtree) self._traverseToMinNode(subtree.left)
def findPath(self): paths = Stack() paths.push(self._startCell) while not paths.isEmpty(): currCell = paths.pop() self._markTried(currCell.row, currCell.col) if currCell.row == self._exitCell.row and currCell.col == self._exitCell.col: currCell.path.append(currCell) for el in currCell.path: self._markPath(el.row, el.col) return True if self._validMove(currCell.row + 1, currCell.col): paths.push( _CellPosition(currCell.row + 1, currCell.col, currCell)) if self._validMove(currCell.row - 1, currCell.col): paths.push( _CellPosition(currCell.row - 1, currCell.col, currCell)) if self._validMove(currCell.row, currCell.col + 1): paths.push( _CellPosition(currCell.row, currCell.col + 1, currCell)) if self._validMove(currCell.row, currCell.col - 1): paths.push( _CellPosition(currCell.row, currCell.col - 1, currCell)) return False
def findPath( self ): traceAcct = Stack() curPos = _CellPosition(self._startCell.row, self._startCell.col) if not self._validMove(curPos.row, curPos.col): return False self._markPath(curPos.row, curPos.col) trapFlag = False while True: for i in range(4): # Left, Up, Right, Down candidatePos = _CellPosition(curPos.row + self.DIRECT[i][0], \ curPos.col + self.DIRECT[i][1]) if self._validMove(candidatePos.row, candidatePos.col): traceAcct.push(curPos) curPos = candidatePos self._markPath(curPos.row, curPos.col) if self._exitFound(curPos.row, curPos.col): return True break if i == 3: trapFlag = True continue if trapFlag: trapFlag = False self._markTried(curPos.row, curPos.col) if traceAcct.isEmpty(): return False else: curPos = traceAcct.pop()
def findPath(self): path_stack = Stack() new = self._startCell path_stack.push(new) while len(path_stack) != 0: new.row = path_stack.peek().row new.col = path_stack.peek().col self._markPath(new.row, new.col) flag_valid = False for i, j in [(-1, 0), (0, 1), (1, 0), (0, -1)]: if self._validMove(path_stack.peek().row + i, path_stack.peek().col + j): new.row = new.row + i new.col = new.col + j path_stack.push(_CellPosition(new.row, new.col)) self._markPath(new.row, new.col) flag_valid = True break if flag_valid: if self._exitFound(new.row, new.col): self._markPath(new.row, new.col) return True continue cell = path_stack.pop() self._markTried(cell.row, cell.col) return False
def find_path(self): """ Attempts to solve the maze by finding a path from the starting cell to the exit. Returns True if a path is found and False otherwise. """ path = Stack() i = self._start_cell.row j = self._start_cell.col while True: if self._exit_found(i, j): self._mark_path(i, j) return True # print(self.__str__()) # print("///////////") for item in ["u", "r", "d", "l", 0]: if not item: if self._exit_found(i, j): self._mark_path(i, j) return True self._mark_tried(i, j) try: dot_1 = path.pop() except AssertionError: return False i = dot_1[0] j = dot_1[1] break else: dot = self.move(i, j, item, path) if dot: i = dot[0] j = dot[1] break
def findPath(self): s = Stack() #stack to save the path s.push(self._startCell) self._markPath(self._startCell.row, self._startCell.col) while True: #inside the loop where neighbor cell is checked for valid move #same time, checked if we get the exit if s.isEmpty(): return False current_cell = s.peek() row = current_cell.row col = current_cell.col if not self._exitFound(row, col): for r in [row - 1, row, row + 1]: if not (current_cell.row == s.peek().row and \ current_cell.col == s.peek().col): break for c in [col - 1, col, col + 1]: if ((r == row) ^ (c == col)): if self._validMove(r, c): self._markPath(r, c) s.push(_CellPosition(r, c)) break if current_cell.row == s.peek().row and\ current_cell.col == s.peek().col: not_in_path = s.pop() self._markTried(not_in_path.row, not_in_path.col) else: return True
def findPath(self): """ finds path in a Maze and Returns True if no path it will return None """ maze_stack = Stack() maze_stack.push(self._startCell) current_cell = self._startCell self._markPath(self._startCell.row, self._startCell.col) while not maze_stack.isEmpty(): possible_moves = [(-1, 0), (0, 1), (1, 0), (0, -1)] current_cell.row = maze_stack.peek().row current_cell.col = maze_stack.peek().col self._markPath(current_cell.row, current_cell.col) moved = False for i, j in possible_moves: if self._validMove(current_cell.row + i, current_cell.col + j): current_cell.row = current_cell.row + i current_cell.col = current_cell.col + j maze_stack.push( _CellPosition(current_cell.row, current_cell.col)) moved = True break if moved: if self._exitFound(current_cell.row, current_cell.col): self._markPath(current_cell.row, current_cell.col) return True continue visited_cell = maze_stack.pop() self._markTried(visited_cell.row, visited_cell.col)
class _BSTMapIterator: def __init__(self, root): # Creates a stack for use in traversing the tree. self._theStack = Stack() self._traverseToMinNode(root) def __iter__(self): return self # Returns the next item from the BST in key order. def __next__(self): # If the stack is empty, we are done. if self._theStack.isEmpty(): raise StopIteration else: # The top node on the stack contains the next key. node = self._theStack.pop() key = node.key # If this node has a subtree rooted as the right child, we must # find the node in that subtree that contains the smallest key. # Again, the nodes along the path are pushed onto the stack. if node.right is not None: self._traverseToMinNode(node.right) return key # Traverses down the subtree to find the node containing the smallest # key during which the nodes along that path are pushed onto the stack. def _traverseToMinNode(self, subtree): if subtree is not None: self._theStack.push(subtree) self._traverseToMinNode(subtree.left)
class _BSTMapIterator: def __init__( self, root ): self._theStack = Stack() # traverse down to the node containing the smallest key during # which each node along the path is pushed onto the stack self._traverseToMinNode( root ) def __iter__( self ): return self # Returns the next item from the BST in key order. def next( self ): # If the stack is empty, we are done.. if self._theStack.isEmpty(): raise StopIteration else: # The top node on the stack contains the key. node = self._theStack.pop() key = node.key # If this node has a subtree rooted as the right child, we must # fiind the node in that subtree that contains the smallest key. # Again, the nodes along the path are pushed onto the stack. if node.right is not None: self._traverseToMinNode( node.right ) return key def _traverseToMinNode( self, subtree ): if subtree is not None: self._theStack.push( subtree ) self._traverseToMinNode( subtree.left )
class _T23Iter: """ Iterator for the 2-3 tree using a software stack """ FIRST_KEY = 0 SECOND_KEY = 1 LEFT_BRANCH = 0 MID_BRANCH = 1 RIGHT_BRANCH = 2 def __init__(self, root): """Create a stack for use in traversing the tree. """ self._theStack = Stack() if root is not None: self._theStack.push((root, _T23Iter.FIRST_KEY)) self._traverseToMinNode(root, _T23Iter.LEFT_BRANCH) def __iter__(self): return self def next(self): """Returns the next item from the BST in key order""" # If the stack is empty, we are done. if self._theStack.isEmpty(): raise StopIteration else: # The top node on the stack contains the next key. (node, which) = self._theStack.pop() if node.isLeaf() and which == _T23Iter.FIRST_KEY: key = node.key1 if node.isFull(): self._theStack.push((node, _T23Iter.SECOND_KEY)) elif node.isLeaf() and which == _T23Iter.SECOND_KEY: key = node.key2 elif not node.isLeaf() and which == _T23Iter.FIRST_KEY: key = node.key1 if node.isFull(): self._theStack.push((node, _T23Iter.SECOND_KEY)) self._traverseToMinNode(node, _T23Iter.MID_BRANCH) else: # not node.isLeaf() and which == _T23Iter.SECOND_KEY: key = node.key2 self._traverseToMinNode(node, _T23Iter.RIGHT_BRANCH) return key def _traverseToMinNode(self, node, branch): """ Traverses down <node>'s branch indecated by <branch> to find the node containing the in-order successor""" if not node.isLeaf(): if branch == _T23Iter.LEFT_BRANCH: node = node.left elif branch == _T23Iter.MID_BRANCH: node = node.middle else: # branch == _T23Iter.RIGHT_BRANCH node = node.right self._theStack.push((node, _T23Iter.FIRST_KEY)) while not node.isLeaf(): node = node.left self._theStack.push((node, _T23Iter.FIRST_KEY))
def printListStack(head): s = Stack() curNode = head while curNode is not None: s.push(curNode.data) curNode = curNode.next while not s.isEmpty(): item = s.pop() print item
def findPath(self): path_stack = Stack() path_stack.push(self._startCell) # print(path_stack.peek().row, path_stack.peek().col) while len(path_stack) > 0: pos_row = path_stack.peek().row pos_col = path_stack.peek().col if self._validMove(pos_row - 1, pos_col): self._mazeCells[pos_row, pos_col] = self.PATH_TOKEN path_stack.push(_CellPosition(pos_row - 1, pos_col)) if self._exitFound(pos_row - 1, pos_col): self._mazeCells[pos_row - 1, pos_col] = self.PATH_TOKEN return True elif self._validMove(pos_row + 1, pos_col): self._mazeCells[pos_row, pos_col] = self.PATH_TOKEN path_stack.push(_CellPosition(pos_row + 1, pos_col)) if self._exitFound(pos_row + 1, pos_col): self._mazeCells[pos_row + 1, pos_col] = self.PATH_TOKEN return True elif self._validMove(pos_row, pos_col - 1): self._mazeCells[pos_row, pos_col] = self.PATH_TOKEN path_stack.push(_CellPosition(pos_row, pos_col - 1)) if self._exitFound(pos_row, pos_col - 1): self._mazeCells[pos_row, pos_col - 1] = self.PATH_TOKEN return True elif self._validMove(pos_row, pos_col + 1): self._mazeCells[pos_row, pos_col] = self.PATH_TOKEN path_stack.push(_CellPosition(pos_row, pos_col + 1)) if self._exitFound(pos_row, pos_col + 1): self._mazeCells[pos_row, pos_col + 1] = self.PATH_TOKEN return True else: self._mazeCells[pos_row, pos_col] = self.TRIED_TOKEN path_stack.pop() return False
def printListStack(head): # Createe a stack to store the items s = Stack() # Iterate throught the list and push each item onto the stack curNode = head while curNode is not None: s.push(curNode.data) curNode = curNode.next # Repeatedly pop the items from the stack and print them until the stack is empty while not s.isEmpty(): item = s.pop() print item
def solveNQueen(self, findAll=False): """ method to solve the n-queens problem. If the findAll flag is set to be true, all solutions will be printed. """ solution = 0 queen_stack = Stack() row = 0 col = 0 while True: while row < self.size() and not self.unguarded(row, col): row += 1 if row == self.size(): if col == 0: # All solutions have been found. return False (row, col) = queen_stack.pop() self.removeQueen(row, col) row += 1 else: self.placeQueen(row, col) queen_stack.push((row, col)) if self.numQueens() == self.size(): if findAll == False: self.draw() return True else: solution = solution + 1 print "Solution %2d found:" % solution self.draw() print "" (row, col) = queen_stack.pop() self.removeQueen(row, col) row += 1 continue row = 0 col += 1
def solveNQueen(self, findAll = False): """ method to solve the n-queens problem. If the findAll flag is set to be true, all solutions will be printed. """ solution = 0 queen_stack = Stack() row = 0 col = 0 while True: while row < self.size() and not self.unguarded(row, col): row += 1 if row == self.size(): if col == 0: # All solutions have been found. return False (row, col) = queen_stack.pop() self.removeQueen(row, col) row += 1 else: self.placeQueen(row, col) queen_stack.push((row, col)) if self.numQueens() == self.size(): if findAll == False: self.draw() return True else: solution = solution + 1 print "Solution %2d found:" % solution self.draw() print "" (row, col) = queen_stack.pop() self.removeQueen(row, col) row += 1 continue row = 0 col += 1
def find_path(self): """ Attempts to solve the maze by finding a path from the starting cell to the exit. Returns True if a path is found and False otherwise. """ maze_path = Stack() first_position = self._start_cell curr_row = first_position.row curr_col = first_position.col maze_path.push(_CellPosition(curr_row, curr_col)) self._mark_path(curr_row, curr_col) while not self._exit_found(curr_row, curr_col): for direction in [(-1, 0), (0, 1), (1, 0), (0, -1)]: new_row = curr_row + direction[0] new_col = curr_col + direction[1] if self._valid_move(new_row, new_col): curr_row = new_row curr_col = new_col maze_path.push(_CellPosition(curr_row, curr_col)) self._mark_path(curr_row, curr_col) break else: continue else: self._mark_tried(curr_row, curr_col) maze_path.pop() if maze_path.is_empty(): return False previous_cell = maze_path.peek() curr_row = previous_cell.row curr_col = previous_cell.col return True
def isValidSource(srcfile): s = Stack() for line in srcfile: for token in line: if token in "{[(": s.push(token) elif token in "}])": if s.isEmpty(): return False else: left = s.pop() if (token == "}" and left != "{") or \ (token == "]" and left != "[") or \ (token == ")" and left != "("): return False return s.isEmpty()
def printListStack( head ): from lliststack import Stack s = Stack() # Iterate through the list and push each item onto the stack curNode = head while curNode is not None: s.push( curNode.data ) curNode = curNode.next # Repeatedly pop the items from the stack and print them while not s.isEmpty(): item = s.pop() print item, print ''
def __delitem__(self, target): assert target is not None, "Invalid map key." keyFound = False nodeTrack = Stack() node = self._root while node is not None: nodeTrack.push(node) if node.hasKey(target): keyFound = True break else: node = node.getBranch(target) if not keyFound: # If target key not found... raise KeyError(target) # If the target node is not a leaf node, swap item with its in-order # successor (always leaf), and then go to the new location of item # to delete. if not nodeTrack.peek().isLeaf(): transNode = nodeTrack.peek() if transNode.key1 == target: node = transNode.middle else: node = transNode.right nodeTrack.push(node) while not node.isLeaf(): node = node.left nodeTrack.push(node) # <node> is anow the leaf node containing the in-order successor # key of <target>. if transNode.key1 == target: transNode.key1, node.key1 = node.key1, transNode.key1 transNode.value1, node.value1 = node.value1, transNode.value1 else: transNode.key2, node.key1 = node.key1, transNode.key2 transNode.value2, node.value1 = node.value1, transNode.value2 leafNode = nodeTrack.pop() if leafNode.isFull(): self._t23DegFull(leafNode, target) else: leafNode.key1 = None leafNode.value1 = None self._t23FixNode(nodeTrack, leafNode) self._size -= 1
def is_validSource(srcfile): s = Stack() for line in srcfile: for token in line: if token in "{[(": s.push(token) elif token in '}])': if s.is_empty(): return False else: left = s.pop() if (token == '}' and left != '{') or \ (token == ']' and left != '[') or \ (token == '(' and left != ')'): return False return s.is_empty()
def find_path(self): """ Attempts to solve the maze by finding a path from the starting cell to the exit. Returns True if a path is found and False otherwise. """ path = Stack() curr_pos = _CellPosition(self._start_cell.row, self._start_cell.col) # self._mark_path(curr_pos.row, curr_pos.col) # path.push(curr_pos) while True: if self._valid_move(curr_pos.row - 1, curr_pos.col): path.push(deepcopy(curr_pos)) self._mark_path(curr_pos.row, curr_pos.col) curr_pos.row, curr_pos.col = curr_pos.row - 1, curr_pos.col # print("up..") elif self._valid_move(curr_pos.row, curr_pos.col + 1): path.push(deepcopy(curr_pos)) self._mark_path(curr_pos.row, curr_pos.col) curr_pos.row, curr_pos.col = curr_pos.row, curr_pos.col + 1 # print("right..") elif self._valid_move(curr_pos.row + 1, curr_pos.col): path.push(deepcopy(curr_pos)) self._mark_path(curr_pos.row, curr_pos.col) curr_pos.row, curr_pos.col = curr_pos.row + 1, curr_pos.col # print("down..") elif self._valid_move(curr_pos.row, curr_pos.col - 1): path.push(deepcopy(curr_pos)) self._mark_path(curr_pos.row, curr_pos.col) curr_pos.row, curr_pos.col = curr_pos.row, curr_pos.col - 1 # print("left..") else: # print("wrong..") self._mark_tried(curr_pos.row, curr_pos.col) old_pos = path.pop() curr_pos.row, curr_pos.col = old_pos.row, old_pos.col # print(curr_pos) if self._exit_found(curr_pos.row, curr_pos.col): self._mark_path(curr_pos.row, curr_pos.col) return True if (curr_pos.row == self._start_cell.row and curr_pos.col == self._start_cell.col): self._mark_tried(curr_pos.row, curr_pos.col) return False
def find_path(self): if not self.maze_cells[self.exit_cell.row, self.exit_cell.col] is None: return False tmp = Stack() i, j = self.start_cell.row, self.start_cell.col dead_ends = [self.CELL_PATH, self.CELL_TRIED, self.CELL_WALL] directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] tmp.push([i, j]) self.maze_cells[i, j] = self.CELL_PATH while True: for direction in directions: try: i1, j1 = i + direction[0], j + direction[1] t = self.maze_cells[i1, j1] except IndexError: continue while self.maze_cells[i1, j1] \ not in dead_ends: i += direction[0] j += direction[1] tmp.push([i, j]) self.maze_cells[i, j] = self.CELL_PATH if i == self.exit_cell.row and j == self.exit_cell.col: return True if i == self.start_cell.row and j == self.start_cell.col: return False if self.check_cell(i, j) is None: while True: coor = tmp.pop() if tmp.isEmpty(): return False self.maze_cells[coor[0], coor[1]] = self.CELL_TRIED coor = tmp.peek() tmpcoor = self.check_cell(coor[0], coor[1]) if tmpcoor: i, j = coor[0], coor[1] break if i == self.start_cell.row and j == self.start_cell.col: return False if i == self.exit_cell.row and j == self.exit_cell.col: return True
def findPath(self): _pathStack = Stack() assert self._startPos is not None, "starting position is not defined" _pathStack.push(self._startPos) _currentCellRow = _pathStack.peek().row _currentCellCol = _pathStack.peek().col while ([_currentCellRow, _currentCellCol] != [self._endPos.row, self._endPos.col]): print("here") _nextPath = False _check = False for i, j in itertools.product(range(-1, 2, 1), repeat=2): if (abs(i - j) == 1): _nextPath = self._validMove(_currentCellRow + i, _currentCellCol + j) if (_nextPath): _check = True print(_currentCellRow + i, _currentCellCol + j, "\n") self._maze[_currentCellRow + i, _currentCellCol + j] = self.MAZE_PATH_TOKEN _pathStack.push( CellPosition(_currentCellRow + i, _currentCellCol + j)) break if (not _check): removedPath = _pathStack.pop() self._maze[removedPath.row, removedPath.col] = self.MAZE_TRIED_TOKEN if (_pathStack is None): return False _currentCellRow = _pathStack.peek().row _currentCellCol = _pathStack.peek().col return True
def findPath(self): stack_path = Stack() stack_path.push(self._startCell) while not stack_path.isEmpty(): use_cell = stack_path.pop() self._markTried(use_cell.row, use_cell.col) if use_cell.row == self._exitCell.row and use_cell.col == self._exitCell.col: use_cell.path.append(use_cell) for i in use_cell.path: self._markPath(i.row, i.col) return True if self._validMove(use_cell.row + 1, use_cell.col): stack_path.push( _CellPosition(use_cell.row + 1, use_cell.col, use_cell)) if self._validMove(use_cell.row - 1, use_cell.col): stack_path.push( _CellPosition(use_cell.row - 1, use_cell.col, use_cell)) if self._validMove(use_cell.row, use_cell.col + 1): stack_path.push( _CellPosition(use_cell.row, use_cell.col + 1, use_cell)) if self._validMove(use_cell.row, use_cell.col - 1): stack_path.push( _CellPosition(use_cell.row, use_cell.col - 1, use_cell)) return False
from lliststack import Stack values = Stack() for i in range(16): if i % 3 == 0: values.push(i) elif i % 4 == 0: values.pop() while not values.isEmpty(): print(values.pop())
class ExtoPostfix: """ Receive an infix expression and turn it into a postfix expression. This is a helper module of postfixCal.py. """ def __init__(self): self._isp = { '#': 0, '(': 1, '*': 5, '/': 5, '+': 3, '-': 3, ')': 6 } # in stack priority self._icp = { '#': 0, '(': 6, '*': 4, '/': 4, '+': 2, '-': 2, ')': 1 } # in coming priority # the list used to save the final postfix expression. self._postfix = [] # the stack used to change the order of operators. self._oprtStack = Stack() def __str__(self): self._postfix = [ elem for elem in self._postfix if elem != ')' and elem != '(' ] return ','.join(self._postfix) def _getPostfix(self, expr): """ Algorithm: 1)initialize the _oprtStack by pushing '#' into it. 2)read in the first element of the infix expr, and assign it to variable ch. 3)repeat the following steps until ch = '#', meanwhile, the top element of the _oprtStack is also '#': a)if ch is operand, append it to the _postfix, and read the next ch; b)if ch is operator, compare the priority(icp) of ch and the priority(isp) of the top element(op) in _oprtStack: if icp(ch) > isp(op), push ch onto _oprtStack, and read another ch if icp(ch) < isp(op), pop the top element in _oprtStack, and append it to _postfix if icp(ch) == isp(op), pop the top element in _oprtStack,if op is '(', read another ch. """ expr = expr.split() self._oprtStack.push('#') ch = expr.pop(0) while self._oprtStack.isEmpty() == False and ch != '#': if ch.isdigit(): self._postfix.append(ch) ch = expr.pop(0) else: ch1 = self._oprtStack.peek() if self._isp[ch1] < self._icp[ch]: self._oprtStack.push(ch) ch = expr.pop(0) elif self._isp[ch1] > self._icp[ch]: out = self._oprtStack.pop() self._postfix.append(out) else: out = self._oprtStack.pop() if out == '(': ch = expr.pop(0) for i in range(len(self._oprtStack) - 1): ch1 = self._oprtStack.pop() self._postfix.append(ch1) def backExpr(self, expr): self._getPostfix(expr) return self._postfix
def find_path(self): """ Attempts to solve the maze by finding a path from the starting cell to the exit. Returns True if a path is found and False otherwise. """ path = Stack() i = self._start_cell.row j = self._start_cell.col counter = 0 while True: check = 0 if self._exit_found(i, j): for _ in range(len(path)): coord = path.pop() self._maze_cells[coord[0], coord[1]] = "x" self._maze_cells[self._exit_cell.row, self._exit_cell.col] = "x" return True if (self._valid_move(i - 1, j) and self._maze_cells[i - 1, j] == None and check == 0): if path.is_empty() or path.peek() != (i - 1, j): path.push((i, j)) counter = 0 check = 1 i -= 1 elif counter == 1: self._maze_cells[i, j] = "o" try: path.pop() except AssertionError: return False counter = 0 check = 1 i -= 1 else: counter += 1 if (self._valid_move(i, j + 1) and self._maze_cells[i, j + 1] == None and check == 0): if path.is_empty() or path.peek() != (i, j + 1): path.push((i, j)) counter = 0 check = 1 j += 1 elif counter == 1: self._maze_cells[i, j] = "o" try: path.pop() except AssertionError: return False counter = 0 check = 1 j += 1 else: counter += 1 if (self._valid_move(i + 1, j) and self._maze_cells[i + 1, j] == None and check == 0): if path.is_empty() or path.peek() != (i + 1, j): path.push((i, j)) counter = 0 check = 1 i += 1 elif counter == 1: self._maze_cells[i, j] = "o" try: path.pop() except AssertionError: return False counter = 0 check = 1 i += 1 else: counter += 1 if (self._valid_move(i, j - 1) and self._maze_cells[i, j - 1] == None and check == 0): if path.is_empty() or path.peek() != (i, j - 1): path.push((i, j)) counter = 0 check = 1 j -= 1 elif counter == 1: self._maze_cells[i, j] = "o" try: path.pop() except AssertionError: return False counter = 0 check = 1 j -= 1 else: counter += 1 if check == 0 and counter == 0: return False
class Maze: # Define constants to represent contents of the maze cells. MAZE_WALL = "%" #* PATH_TOKEN = "." #x TRIED_TOKEN = "-" #o # Creates a maze object with all cells marked as open. def __init__(self, numRows, numCols): self._mazeCells = Array2D(numRows, numCols) self._startCell = None self._exitCell = None self._path = Stack() self._path_found = False # Returns the number of rows in the maze. def numRows(self): return self._mazeCells.numRows() # Returns the number of columns in the maze. def numCols(self): return self._mazeCells.numCols() # Fills the indicated cell with a "wall" marker. def setWall(self, row, col): assert row >= 0 and row < self.numRows() and \ col >= 0 and col < self.numCols(), "Cell index out of range." self._mazeCells.__setitem__((row, col), self.MAZE_WALL) # Sets the starting cell position. def setStart(self, row, col): assert row >= 0 and row < self.numRows() and \ col >= 0 and col < self.numCols(), "Cell index out of range." self._startCell = _CellPosition(row, col) # Sets the exit cell position. def setExit(self, row, col): assert row >= 0 and row < self.numRows() and \ col >= 0 and col < self.numCols(), \ "Cell index out of range." self._exitCell = _CellPosition(row, col) # Attempts to solve the maze by finding a path from the starting cell # to the exit. Returns True if a path is found and False otherwise. def findPath(self): start = (self._startCell.row, self._startCell.col) end = (self._exitCell.row, self._exitCell.col) # If start is the same as end, then there's an error if start == end: return False self._path.push(start) self.make_a_move() return self._path_found def make_a_move(self): # Keep recursion going until stack is empty from not # finding a path if self._path._size > 0: cur_row, cur_col = self._path.peek() # If the current cell is also the exit cell # Break out of the recursion and return True! if self._exitFound(cur_row, cur_col): self._markPath(cur_row, cur_col) self._path_found = True return # Checks to see if any of the surrounding cells are # valid to move into- up, right, bottom, left # If there are valid moves available, move into one if (self._validMove(cur_row - 1, cur_col) or \ self._validMove(cur_row, cur_col + 1) or \ self._validMove(cur_row + 1, cur_col) or \ self._validMove(cur_row, cur_col - 1)): # Mark the current cell we're on as 'tried' self._markPath(cur_row, cur_col) # We will now go for one of the next cells if self._validMove(cur_row - 1, cur_col): self._path.push((cur_row - 1, cur_col)) self.make_a_move() elif self._validMove(cur_row, cur_col + 1): self._path.push((cur_row, cur_col + 1)) self.make_a_move() elif self._validMove(cur_row + 1, cur_col): self._path.push((cur_row + 1, cur_col)) self.make_a_move() elif self._validMove(cur_row, cur_col - 1): self._path.push((cur_row, cur_col - 1)) self.make_a_move() # Otherwise, we are at a dead end and must backtrace else: self._markTried(self._path.peek()[0], self._path.peek()[1]) self._path.pop() self.make_a_move() # Resets the maze by removing all "path" and "tried" tokens. #def reset( self ): #...... # Prints a text-based representation of the maze. def draw(self): # For each row for row, row_ndx in enumerate(range(self.numRows())): row_display = '' # Iterate through the number of columns there are # And then print the (row) value for col, col_ndx in enumerate(range(self.numCols())): if self._mazeCells.__getitem__((row_ndx, col_ndx)) == None: row_display += ' ' else: row_display += str( self._mazeCells.__getitem__((row_ndx, col_ndx))) print row_display # Returns True if the given cell position is a valid move. def _validMove(self, row, col): return row >= 0 and row < self.numRows() \ and col >= 0 and col < self.numCols() \ and self._mazeCells[row, col] is None # Helper method to determine if the exit was found. def _exitFound(self, row, col): return row == self._exitCell.row and \ col == self._exitCell.col # Drops a "tried" token at the given cell. def _markTried(self, row, col): self._mazeCells.__setitem__((row, col), self.TRIED_TOKEN) # Drops a "path" token at the given cell. def _markPath(self, row, col): self._mazeCells.__setitem__((row, col), self.PATH_TOKEN)
# Test the Stack #from liststack import Stack from lliststack import Stack PROMPT = "Enter an int value (<0 to end): " myStack = Stack() value = int(input( PROMPT )) while value >= 0: myStack.push( value ) value = int(input( PROMPT )) while not myStack.isEmpty(): value = myStack.pop() print( value )
class Calculator: """ A simple calculator which process a postfix expr and return the result of the arithmetic expr. """ def __init__(self, expr): # _expr is a list, which contains the split of the expr. self._expr = [] # _stack is a stack, whick is a main structure in the processing. self._stack = Stack() # split the expr string into a list, which turn operands into floats # and remain the operators #as string. for elem in expr: if elem not in ['+', '-', '*', '/']: self._expr.append(float(elem)) else: self._expr.append(elem) def _calResult(self): """ _calResult is the main process routine which calculate the result of the expr: 1.If the current item is an operand, push its value onto the stack. 2.If the current item is an operator: a)Pop the top two operands off the stack. b)Perform the operation.(Note the top value is the right operand while the next to the top value is the left operand. c)Push the reuslt of the operation back onto the stack. Invalid Exprs: a)More operands than operators: If the stack is empty, we can stop the evaluation and flag an error. b)More operators than operands: If the stack is not empty(at the end), the expression was invalid and we must flag an error. Actually, we can check the size of the stack when all the elements in _expr is used.If it equals 1, the expr is valid, otherwise invalid. """ for elem in self._expr: if elem not in ['+', '-', '*', '/']: self._stack.push(elem) else: try: roprd = self._stack.pop() loprd = self._stack.pop() if elem == '+': self._stack.push(loprd + roprd) elif elem == '-': self._stack.push(loprd - roprd) elif elem == '*': self._stack.push(loprd * roprd) else: self._stack.push(loprd / roprd) # AssertionError indicates the _stack is already empty, expr # invalid. except AssertionError as AssErr: print "The expression you input is wrong(too many operators). Please check it again." print "Error Message:", AssErr.message if AssErr.message else "None" sys.exit() # used for division operators, zero can not be used as an # divident. except ZeroDivisionError as DivErr: print "Zero can not used as an divident." print "Error Message:", DivErr.message if DivErr.message else "None" sys.exit() except Exception as ExcErr: # catch the unexpected exceptions. print "Unknown Error." print "Error Message", ExcErr.message if ExcErr.message else "None" sys.exit() try: # at the end of the for loop, check the size of _stack, if equals # 1, valid, otherwise invalid. if len(self._stack) != 1: raise AssertionError else: result = self._stack.pop() return result except AssertionError as AssErr: print "The expression you input is wrong(too many operands). Please check it again." print "Error Message:", AssErr.message if AssErr.message else "None" sys.exit() def backResult(self): """ Format the result of the expr. """ self._calResult() print "The result of the arithmetic is:%.2f" % self._calResult()
class Maze: # Define constants to represent contents of the maze cells. MAZE_WALL = " *" PATH_TOKEN = " x" TRIED_TOKEN = " o" # Creates a maze object with all cells marked as open. def __init__(self, num_rows, num_cols): self.stack = Stack() self._mazeCells = Array2D(num_rows, num_cols) self._startCell = None self._exitCell = None self.st = Stack() # Returns the number of rows in the maze. def num_rows(self): return self._mazeCells.num_rows() # Returns the number of columns in the maze. def num_cols(self): return self._mazeCells.num_cols() # Fills the indicated cell with a "wall" marker. def setWall(self, row, col): assert 0 <= row < self.num_rows() and \ 0 <= col < self.num_cols(), "Cell index out of range." self._mazeCells[row, col] = self.MAZE_WALL # Sets the starting cell position. def setStart(self, row, col): assert 0 <= row < self.num_rows() and \ 0 <= col < self.num_cols(), "Cell index out of range." self._startCell = _CellPosition(row, col) # Sets the exit cell position. def setExit(self, row, col): assert 0 <= row < self.num_rows() and \ 0 <= col < self.num_cols(), "Cell index out of range." self._exitCell = _CellPosition(row, col) # Method to find neighbour cells def neighbours(self, _row, _col): info = [] for coord in [(1, 0), (0, 1), (-1, 0), (0, -1)]: row, col = _row + coord[0], _col + coord[1] if self._validMove(row, col): if not self._mazeCells[row, col]: info.append(_CellPosition(row, col)) return info # Attempts to solve the maze by finding a path from the starting cell # to the exit. Returns True if a path is found and False otherwise. def findPath(self): self.stack.push(self._startCell) while len(self.stack): current = self.stack.peek() row, col = current.row, current.col if self._exitFound(row, col): self._markPath(row, col) return True fine_cells = self.neighbours(row, col) if fine_cells: self._markPath(row, col) for element in fine_cells: self.stack.push(element) else: self._markTried(row, col) self.stack.pop() # Resets the maze by removing all "path" and "tried" tokens. def reset(self): for row in range(self._mazeCells.num_rows()): for col in range(self._mazeCells.num_cols()): if self._mazeCells[row, col] in [" x", " o"]: self._mazeCells[row, col] = None # Prints a text-based representation of the maze. def draw(self): for row in range(self._mazeCells.num_rows()): for col in range(self._mazeCells.num_cols()): if self._mazeCells[row, col]: print(self._mazeCells[row, col], end=" ") else: print(" ", end=" ") print() # Returns True if the given cell position is a valid move. def _validMove(self, row, col): return 0 <= row < self.num_rows() \ and 0 <= col < self.num_cols() \ and self._mazeCells[row, col] is None # Helper method to determine if the exit was found. def _exitFound(self, row, col): return row == self._exitCell.row and col == self._exitCell.col # Drops a "tried" token at the given cell. def _markTried(self, row, col): self._mazeCells[row, col] = self.TRIED_TOKEN # Drops a "path" token at the given cell. def _markPath(self, row, col): self._mazeCells[row, col] = self.PATH_TOKEN
class Maze: # Define constants to represent contents of the maze cells. MAZE_WALL = "%" # * PATH_TOKEN = "." # x TRIED_TOKEN = "-" # o # Creates a maze object with all cells marked as open. def __init__(self, numRows, numCols): self._mazeCells = Array2D(numRows, numCols) self._startCell = None self._exitCell = None self._path = Stack() self._path_found = False # Returns the number of rows in the maze. def numRows(self): return self._mazeCells.numRows() # Returns the number of columns in the maze. def numCols(self): return self._mazeCells.numCols() # Fills the indicated cell with a "wall" marker. def setWall(self, row, col): assert row >= 0 and row < self.numRows() and col >= 0 and col < self.numCols(), "Cell index out of range." self._mazeCells.__setitem__((row, col), self.MAZE_WALL) # Sets the starting cell position. def setStart(self, row, col): assert row >= 0 and row < self.numRows() and col >= 0 and col < self.numCols(), "Cell index out of range." self._startCell = _CellPosition(row, col) # Sets the exit cell position. def setExit(self, row, col): assert row >= 0 and row < self.numRows() and col >= 0 and col < self.numCols(), "Cell index out of range." self._exitCell = _CellPosition(row, col) # Attempts to solve the maze by finding a path from the starting cell # to the exit. Returns True if a path is found and False otherwise. def findPath(self): start = (self._startCell.row, self._startCell.col) end = (self._exitCell.row, self._exitCell.col) # If start is the same as end, then there's an error if start == end: return False self._path.push(start) self.make_a_move() return self._path_found def make_a_move(self): # Keep recursion going until stack is empty from not # finding a path if self._path._size > 0: cur_row, cur_col = self._path.peek() # If the current cell is also the exit cell # Break out of the recursion and return True! if self._exitFound(cur_row, cur_col): self._markPath(cur_row, cur_col) self._path_found = True return # Checks to see if any of the surrounding cells are # valid to move into- up, right, bottom, left # If there are valid moves available, move into one if ( self._validMove(cur_row - 1, cur_col) or self._validMove(cur_row, cur_col + 1) or self._validMove(cur_row + 1, cur_col) or self._validMove(cur_row, cur_col - 1) ): # Mark the current cell we're on as 'tried' self._markPath(cur_row, cur_col) # We will now go for one of the next cells if self._validMove(cur_row - 1, cur_col): self._path.push((cur_row - 1, cur_col)) self.make_a_move() elif self._validMove(cur_row, cur_col + 1): self._path.push((cur_row, cur_col + 1)) self.make_a_move() elif self._validMove(cur_row + 1, cur_col): self._path.push((cur_row + 1, cur_col)) self.make_a_move() elif self._validMove(cur_row, cur_col - 1): self._path.push((cur_row, cur_col - 1)) self.make_a_move() # Otherwise, we are at a dead end and must backtrace else: self._markTried(self._path.peek()[0], self._path.peek()[1]) self._path.pop() self.make_a_move() # Resets the maze by removing all "path" and "tried" tokens. # def reset( self ): # ...... # Prints a text-based representation of the maze. def draw(self): # For each row for row, row_ndx in enumerate(range(self.numRows())): row_display = "" # Iterate through the number of columns there are # And then print the (row) value for col, col_ndx in enumerate(range(self.numCols())): if self._mazeCells.__getitem__((row_ndx, col_ndx)) == None: row_display += " " else: row_display += str(self._mazeCells.__getitem__((row_ndx, col_ndx))) print row_display # Returns True if the given cell position is a valid move. def _validMove(self, row, col): return ( row >= 0 and row < self.numRows() and col >= 0 and col < self.numCols() and self._mazeCells[row, col] is None ) # Helper method to determine if the exit was found. def _exitFound(self, row, col): return row == self._exitCell.row and col == self._exitCell.col # Drops a "tried" token at the given cell. def _markTried(self, row, col): self._mazeCells.__setitem__((row, col), self.TRIED_TOKEN) # Drops a "path" token at the given cell. def _markPath(self, row, col): self._mazeCells.__setitem__((row, col), self.PATH_TOKEN)