def test_pop_to_empty(): stack = Stack() stack.push(1) stack.push(2) stack.pop() stack.pop() assert stack.is_empty
def test_peek_empty_exception(): stack = Stack() stack.push(1) stack.pop() actual = stack.peek() expected = "empty stack" assert actual == expected
def test_pop_one(): stack = Stack() stack.push(1) stack.pop() expected = None actual = stack.peek() assert actual == expected
def test_mult_pop_stack(): stack = Stack() stack.push('1') stack.push('2') stack.pop() stack.pop() assert stack.is_empty() == True
def test_stack_pop_all_top(): hats = Stack() hats.push('seahawks') hats.push('gray') hats.pop() hats.pop() assert hats.top is None
def test_stack_exception(): stack = Stack() exc = "Method not allowed on empty collection." with pytest.raises(InvalidOperationError) as error1: stack.peek() with pytest.raises(InvalidOperationError) as error2: stack.pop() assert str(error1.value) == exc and str(error2.value) == exc
def test_stack_pop_until_empty(): s = Stack() s.push('1') s.push('2') assert s.pop() == '2' assert s.pop() == '1' assert s.pop() == None
def test_stack_pop_all_top(): colors = Stack() colors.push('red') colors.push('blue') colors.pop() colors.pop() assert colors.top is None
def test_pop_off(): stack = Stack() stack.push(1) stack.push(2) stack.pop() expected = 1 actual = stack.peek() assert actual == expected
def test_stack_pop_one(): hats = Stack() hats.push('seahawks') hats.push('gray') hats.push('tan') hats.push('black') hats.pop() assert hats.top.data == 'tan'
def test_stack_pop_one(): colors = Stack() colors.push('red') colors.push('yellow') colors.push('orange') colors.push('green') colors.pop() assert colors.top.data == 'orange'
def dequeue(self): rev_stack = Stack() while self.stack.top: rev_stack.push(self.stack.pop()) removed = rev_stack.pop() while rev_stack.top: self.enqueue(rev_stack.pop()) return removed
def test_multipop(): test_stack = Stack() test_stack.push(1) test_stack.push(2) test_stack.push(3) test_stack.pop() test_stack.pop() test_stack.pop() assert test_stack.length == 0 assert test_stack.bottom == None
def test_stack_pop_three(): colors = Stack() colors.push('red') colors.push('orange') colors.push('green') colors.push('blue') colors.pop() colors.pop() colors.pop() assert colors.top.data == 'red'
def test_pop_until_empty(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") s.pop() s.pop() s.pop() actual = s.is_empty() expected = True assert actual == expected
def test_stack_pop_many(): s = Stack() s.push('1') s.push('2') s.push('3') s.push('4') assert s.pop() == '4' assert s.pop() == '3' assert s.pop() == '2' assert s.pop() == '1'
def test_pop_some(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") s.pop() actual = s.pop() expected = "banana" assert actual == expected
def test_pop(): """ Can successfully pop off the stack Can successfully empty a stack after multiple pops. """ fruits = Stack() fruits.push('apple') fruits.push('pear') fruits.pop() fruits.pop() assert 'No value.' == fruits.peek_stack()
def depth_first(self,node): stack = Stack() stack.push(node) seen =[] while stack.peek_stack() is not None: current = stack.pop() if current.visited == False: seen.append(current.value) current.visited = True for i in current.list: stack.push(i[0]) if current.visited is False: stack.pop() return seen
def depth_first(self, starting_vertex): """ Method to do depth-first traversal on a graph. Input: starting vertex Output: list of vertices in the depth-first order """ vertices = [] depth = Stack() if starting_vertex not in self._adjacency_list: raise ValueError depth.push(starting_vertex) while not depth.is_empty(): top_vertex = depth.pop() vertices.append(top_vertex.value) top_node_neighbors = self.get_neighbors(top_vertex) for neighbor in top_node_neighbors[::-1]: if not neighbor[0].visited: top_vertex.visited = True neighbor[0].visited = True depth.push(neighbor[0]) for node in self._adjacency_list: node.visited = False return vertices
class PseudoQueue(object): # internal stack obj _data = None def __init__(self): # create Stack for internal data-struct self._data = Stack() def count(self): # pass through method to underlying data struct # BigO == O(n) return self._data.count() def enqueue(self, val: str) -> bool: # enqeue a value at the end queue # BigO == O(1) self._data.push(val) return True def dequeue(self) -> (str, bool): # dequeue from head of queue # BigO == O(n) # Algo: use a second stack, as we need the bottom element on the first stack # so we are going to unload the first stack, into a temp stack, in order to # get at the bottom element of the first, then rebuild it from temp to first-stack retStr = '' retBool = False # Empty List? Early return! b, s = self._data.peek() if not b: return retStr, retBool # reverse the primary stack into temp stack tempStack = Stack() while True: b, s = self._data.peek() if b == False: break val = self._data.pop() tempStack.push(val) # top element on tempstack is the bottom of the primary data stack retStr = tempStack.pop() # reverse the temp stack back to the primary stack while True: b, s = tempStack.peek() if b == False: break val = tempStack.pop() self._data.push(val) return retStr, True
def test_pop(): test_stack = Stack() test_stack.push(1) test_stack.push(2) test_stack.push(3) popped = test_stack.pop() assert popped == 3 assert test_stack.length == 2 assert test_stack.top.value == 2
def test_empty_stack_pop(): """Can successfully notify if a stack is empty before popping. """ laundry = Stack() expected = 'The stack is empty' actual = laundry.pop() assert expected == actual
class PseudoQueue: def __init__(self): self.stack1 = Stack() self.stack2 = Stack() def enqueue(self,data): self.stack1.push(data) # self.stack2.push(data) def dequeue(self): if self.stack1.is_empty() and self.stack2.is_empty(): raise AttributeError('The PseudoQueue is empty') if self.stack2.is_empty(): while self.stack1.top != None: self.stack2.push(self.stack1.pop()) return self.stack2.pop() else: return self.stack2.pop()
def test_one_pop(): """Can successfully pop an item off a stack. """ laundry = Stack() laundry.push('socks') laundry.push('jeans') laundry.push('shirt') expected = 'shirt' actual = laundry.pop() assert expected == actual
class PseudoQueue: def __init__(self): self.read_stack = Stack() self.hold_stack = Stack() def enqueue(self, value): self.read_stack.push(value) def dequeue(self): current = self.read_stack.top while current.next: node = self.read_stack.pop() self.hold_stack.push(node) current = current.next front = self.read_stack.pop() current = self.hold_stack.top while current: node = self.hold_stack.pop() self.read_stack.push(node) current = current.next return front
def dequeue(self, pref: AnimalType = None) -> Animal: # grab animal that has been in queue the longest, optionally provide parameter # No Pref First if pref == None: b, val = self.q.peek() if not b: return None if self.q.dequeue(val): return Animal.Factory(val) else: stack = Stack() # Find the Animal we Want (deq, push to stack) found = False b, val = self.q.peek() while b: animal = Animal.Factory(val) if animal.animaltype == pref: found = True break stack.push(val) self.q.dequeue(val) b, val = self.q.peek() if found: # Pop off stack and enqueue back to queue # record the current count, as we need to cycle the enq (back of line) to the front c = self.q.count() b, val = stack.peek() while b: val = stack.pop() self.q.enqueue(val) b, val = stack.peek() # cycle the back of the list to the front diff = self.q.count() - c for x in range(diff): val = self.q.peek() self.q.dequeue(val) self.q.enqueue(val) return animal else: return None
def depth_first(self, node): nodes = [] depth = Stack() depth.push(node) visited = {node: True} while not depth.is_empty(): front = depth.pop() nodes.append(front.value) for el in self.adjacency_list[front]: if el.node not in visited: visited[el.node] = True depth.push(el.node) return nodes
def dequeue(self) -> (str, bool): # dequeue from head of queue # BigO == O(n) # Algo: use a second stack, as we need the bottom element on the first stack # so we are going to unload the first stack, into a temp stack, in order to # get at the bottom element of the first, then rebuild it from temp to first-stack retStr = '' retBool = False # Empty List? Early return! b, s = self._data.peek() if not b: return retStr, retBool # reverse the primary stack into temp stack tempStack = Stack() while True: b, s = self._data.peek() if b == False: break val = self._data.pop() tempStack.push(val) # top element on tempstack is the bottom of the primary data stack retStr = tempStack.pop() # reverse the temp stack back to the primary stack while True: b, s = tempStack.peek() if b == False: break val = tempStack.pop() self._data.push(val) return retStr, True
class PseudoQueue: def __init__(self): self.stack = Stack() def enqueue(self, value): self.stack.push(value) def dequeue(self): rev_stack = Stack() while self.stack.top: rev_stack.push(self.stack.pop()) removed = rev_stack.pop() while rev_stack.top: self.enqueue(rev_stack.pop()) return removed