class ManufacturingProcess(Queue, Stack): # 제조 프로세스 def __init__(self, typ): # setting waitingLine : Queue or Stack if typ == 'queue': self.waitingLine = Queue() if typ == 'stack': self.waitingLine = Stack() def arriveProduct(self, plan): self.waitingLine.add(plan) def leaveProduct(self): if self.getSize() > 0: plan = self.waitingLine.get() # get() : remove할 대상을 불러온다. else: plan = 'none' # none 설정해야 한다 return plan def getSize(self): size = self.waitingLine.getSize() # Queue of Stack을 통해서 구현 , return size def getListString(self): String = self.waitingLine.getListString() return String
class ManufacturingProcess(Queue, Stack): def __init__(self, typ): if typ == 'queue': self.waitingLine = Queue() if typ == 'stack': self.waitingLine = Stack() def arriveProduct(self, plan): # Problem 4. complete the method call to add a product to the waiting line self.waitingLine.add(plan) def leaveProduct(self): if self.getSize() > 0: plan = self.waitingLine.get() else: plan = 'none' return plan def getSize(self): size = self.waitingLine.getSize() return size def getListString(self): String = self.waitingLine.getListString() return String
def change(self, sequence, sequence2): ## Get to origin tree node = self.root # Reference to traverse tree parent = None # Auxiliar if node must be deleted stack = Stack() # Stack to trim tree for char in sequence: parent = node stack.add(node) node = node.travel(char) # If path is not built if (node == None): return ## Get to destination sequence node2 = self.root # Reference to traverse tree parent2 = None # Auxiliar if node must be deleted new = False for char in sequence2: parent2 = node2 node2 = node2.travel(char) # If path is not built if (node2 == None): new = True # Build A path if (char == 'A'): parent2.setA(Node()) node2 = parent2.getA() # Build T path if (char == 'T'): parent2.setT(Node()) node2 = parent2.getT() ## Check if valid method if not new: return "ERROR: Cannot CHANGE. Use CHANGEMERGE instead." ## Disconnect origin tree if parent.getA() == node: parent.setA(None) if parent.getT() == node: parent.setT(None) ## Reconnect tree on destination if parent2.getA() == node2: parent2.setA(node) if parent2.getT() == node2: parent2.setT(node) ## Trim origin BinTree.trimTree(stack) return None
def changeMerge(self, sequence, sequence2): ## Get to origin tree node = self.root # Reference to traverse tree parent = None # Auxiliar if node must be deleted stack = Stack() # Stack to trim tree for char in sequence: parent = node stack.add(node) node = node.travel(char) # If path is not built if (node == None): return stack.unstack() # Should not trim last node ## Get to destination sequence node2 = self.root # Reference to traverse tree parent2 = None # Auxiliar if node must be deleted for char in sequence2: parent2 = node2 node2 = node2.travel(char) # If path is not built if (node2 == None): # Build A path if (char == 'A'): parent2.setA(Node()) node2 = parent2.getA() # Build T path if (char == 'T'): parent2.setT(Node()) node2 = parent2.getT() ## Disconnect origin tree if parent.getA() == node: parent.setA(None) if parent.getT() == node: parent.setT(None) ## Merge trees if parent2.getA() == node: char = 'A' if parent2.getT() == node: char = 'T' BinTree.mergeTrees(node, node2, parent2, char) ## Trim origin BinTree.trimTree(stack)
class StackTest(TestCase): def setUp(self): self.stack = Stack() def test_add(self): """tests the add method in Stack""" self.stack.add(1) self.assertEqual([1], self.stack.stack) def test_remove(self): """tests the remove method in Stack""" self.stack.stack = [2, 3, 1, 6, 9] self.stack.remove() self.assertEqual([2, 3, 1, 6], self.stack.stack)
def delete(self, sequence): node = self.root # Reference to traverse tree stack = Stack() # Stack to trim tree for char in sequence: stack.add(node) node = node.travel(char) # If path is not built if (node == None): return "ERROR: Cannot DELETE." stack.add(node) node.setValue(0) BinTree.trimTree(stack) return None
def DFS(self): if len(self.nodes) == 0: return [] root = self.nodes[0] visited = set([root]) S = Stack() S.add(root) DfsResult = [] while S.size() > 0: StackTop = S.remove() DfsResult.append(StackTop) for neighbour in StackTop.neighbours: if neighbour not in visited: S.add(neighbour) visited.add(neighbour) return DfsResult
def main(): stack = Stack() stack.add("Mon") stack.add("Tue") print(stack.peek()) print(stack.bottom()) print(stack.size()) print("\n") stack.add("Wed") stack.add("Thu") print(stack.peek()) print(stack.bottom()) print(stack.size()) print("\n") stack.remove() print(stack.peek()) print(stack.bottom()) print(stack.size())
def set(self, sequence, value): node = self.root # Reference to traverse tree parent = None # Auxiliar if path must be created stack = Stack() # Stack to trim tree for char in sequence: parent = node stack.add(node) node = parent.travel(char) # If path is not built if (node == None): # Build A path if (char == 'A'): parent.setA(Node()) node = parent.getA() # Build T path if (char == 'T'): parent.setT(Node()) node = parent.getT() stack.add(node) node.setValue(value) BinTree.trimTree(stack)
def generate_maze(self): """generates a maze into the grid""" #creating two stacks. One for columns and on for rows coordinates c_stack = Stack() r_stack = Stack() #created a visited list, initializing the starting node, marking it True(aka visited) and adding its coordinates to each of the stacks visited = np.zeros((self.grid_size[0], self.grid_size[1]), dtype = np.bool) c = 0 r = 0 visited[r][c] = True c_stack.add(c) r_stack.add(r) #starting with the starting node, then cheking its neighbors, if there are any choosing one at random. #If there are not, backtraking to a node that has neightbors. (this will end once it backtracks to the starting now aka when the stack is empty) while len(c_stack.stack) >= 0: #creating the neighbors list and adding the neightbors of the node to it neighbors = self.get_neighbors(r, c, visited) # choosing a neighbor at random if there are any neighbors, displaying it, adding the coordinates to the stack, making the current node's locaions right if len(neighbors) > 0: choice = random.choice(neighbors) if choice == "right": self.grid[r][c + 1] = [10, 206, 245] c += 2 visited[r][c] = True c_stack.add(c) r_stack.add(r) elif choice == "left": self.grid[r][c - 1] = [10, 206, 245] c -= 2 visited[r][c] = True c_stack.add(c) r_stack.add(r) elif choice == "up": self.grid[r - 1][c] = [10, 206, 245] r -= 2 visited[r][c] = True c_stack.add(c) r_stack.add(r) else: self.grid[r + 1][c] = [10, 206, 245] r += 2 visited[r][c] = True c_stack.add(c) r_stack.add(r) #if there are no elements in the stack elif len(c_stack.stack) == 0: break #if there are not neighbors else: c = c_stack.remove() r = r_stack.remove()
def getAll(self): stack = Stack() # Stack for tree traversing sequence = "" # Name of the actual traversing sequence status = 0 # Indicator of the traversing direction output = "" # String output stack.add(self.root) elem = self.root # Node Reference last = self.root # Auxiliar Node Reference """ Tree Traversing is manually done using a stack and statuses to identify the actual traversing status in an iterative manner. The conditions used for a pre-order traversing are: last == stack.top().getT(): switch to status 2 (go to backward traversing) Status == 0: process element and stack A (switch to 1 if not) Status == 1: stack T and switch to 0 (switch to 2 if not) Status == 2: unstack element and switch to 1 (backward traversing) """ # Process Tree while (not stack.isEmpty()): elem = stack.top() if last == stack.top().getT(): last = self.root status = 2 continue if status == 0: # Process element if sequence != "" and elem.getValue() != 0: output += sequence + " " + str(elem.getValue()) + "\n" # Can't stack A if elem.getA() == None: status = 1 continue # Stack A sequence += 'A' stack.add(elem.getA()) if status == 1: # Can't stack T if elem.getT() == None: status = 2 continue # Stack T sequence += 'T' stack.add(elem.getT()) status = 0 if status == 2: # Unstack sequence = sequence[:-1] last = stack.unstack() status = 1 # End of Tree Processing # Clean Output last newline output = output[:-1] return output