def test_peek(): stack = Stack() stack.push(1) stack.push(2) actual = stack.peek() expected = 2 assert actual == expected
def test_peek(): s = Stack() s.push("apple") s.push("banana") actual = s.peek() expected = "banana" assert actual == expected
def test_check_not_empty(): s = Stack() s.push("apple") s.push("banana") actual = s.is_empty() expected = False assert actual == expected
def test_peek_empty_exception(): stack = Stack() stack.push(1) stack.pop() actual = stack.peek() expected = "empty stack" assert actual == expected
def test_peek(): test_stack = Stack() test_stack.push(1) test_stack.push(2) test_stack.push(3) assert test_stack.peek() == 3
def test_stack_pop(): testing_stack = Stack() testing_stack.push(1) testing_stack.push(2) expected = 2 actual = testing_stack.peek() assert actual == expected
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
def test_mult_pop_stack(): stack = Stack() stack.push('1') stack.push('2') stack.pop() stack.pop() assert stack.is_empty() == True
def test_pop_one(): stack = Stack() stack.push(1) stack.pop() expected = None actual = stack.peek() assert actual == expected
def test_pop_to_empty(): stack = Stack() stack.push(1) stack.push(2) stack.pop() stack.pop() assert stack.is_empty
def test_stack_peek(): """Can successfully peek the next item on the stack. """ laundry = Stack() laundry.push('socks') laundry.push('jeans') assert laundry.top.value == 'jeans'
def test_stack_push_multiple(): testing_stack = Stack() testing_stack.push(1) testing_stack.push(2) testing_stack.push(3) expected = 3 actual = testing_stack.peek() assert actual == expected
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_stack_pop_all_top(): hats = Stack() hats.push('seahawks') hats.push('gray') hats.pop() hats.pop() assert hats.top is None
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_push_multiple(): stack = Stack() stack.push(1) stack.push(2) stack.push(3) expected = 3 actual = stack.peek() assert actual == expected
def test_stack_push_mult(): s = Stack() s.push('1') s.push('2') s.push('3') s.push('4') assert s.top.value == '4'
def test_pop_off(): stack = Stack() stack.push(1) stack.push(2) stack.pop() expected = 1 actual = stack.peek() assert actual == expected
def test_push_multiple(): stack = Stack() stack.push('A') stack.push('B') stack.push('C') expected = 'C' actual = stack.peek() assert expected == actual
def test_stack_push(): # STACK: push onto a stack stack = Stack() stack.push('pao de queijo') actualBool, actualStr = stack.peek() expectedBool, expectedStr = True, 'pao de queijo' assert actualBool == expectedBool assert actualStr == expectedStr
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
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 stack_of_seven(): new_stack = Stack() new_stack.push('One') new_stack.push('Two') new_stack.push('Three') new_stack.push('Four') new_stack.push('Five') new_stack.push('Six') new_stack.push('Seven') return new_stack
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 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_push(): """Can successfully push a node on a stack. """ laundry = Stack() laundry.push('socks') expected = 'socks' actual = laundry.top.value assert expected == actual
def test_more_push(): new_stack = Stack() new_stack.push('One') new_stack.push('Two') new_stack.push('Three') new_stack.push('Four') new_stack.push('Five') new_stack.push('Six') new_stack.push('Seven') assert new_stack.top.value == 'Seven'
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_push(): """Can successfully push multiple nodes on a stack. """ laundry = Stack() laundry.push('socks') laundry.push('jeans') laundry.push('shirt') expected = 'shirt' actual = laundry.top.value assert expected == actual
def test_two_pop(): """Can successfully pop all items off a stack. """ laundry = Stack() laundry.push('socks') laundry.push('jeans') laundry.push('shirt') expected = 'socks' actual = laundry.pop_all() assert expected == actual