def test_stack_pop():
    testing_stack = Stack()
    testing_stack.push(1)
    testing_stack.push(2)
    expected = 2
    actual = testing_stack.peek()
    assert actual == expected
Exemple #2
0
def test_peek():
    stack = Stack()
    stack.push(1)
    stack.push(2)
    actual = stack.peek()
    expected = 2
    assert actual == expected
def test_if_stack_is_empty_boolean(stack):
    """Returns boolean is stack is empty"""
    empty_stack = Stack()
    stack.push('mouse')
    actual = [empty_stack.is_empty(), stack.is_empty()]
    expected = [True, False]
    assert actual == expected
Exemple #4
0
def prep_stack():
    elements = Stack()
    elements.push('The challenge')
    elements.push('has been')
    elements.push('solved')
    elements.push('successfully')
    return elements
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():
    # 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_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
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 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_push():
    """Can successfully push a node on a stack.
    """
    laundry = Stack()
    laundry.push('socks')

    expected = 'socks'
    actual = laundry.top.value

    assert expected == actual
Exemple #11
0
def test_empty_stack():
  """
  Exception raised when peeking empty stack.
  """

  stack = Stack()
  
  try:
    stack.peek()
  except EmptyStackException:
    assert True
def test_push_one():
    """
    Can successfully push onto a stack
    Can successfully peek the next item on the stack.
    """

    fruits = Stack()

    fruits.push('apple')

    expected = 'apple'

    assert expected == fruits.peek_stack()
def test_many_push():
    new_stack = Stack()
    new_stack.push('Penny')
    new_stack.push('Honey')
    new_stack.push('Lacey')
    new_stack.push('Pickles')
    assert new_stack.top.value == 'Pickles'
Exemple #14
0
 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
Exemple #15
0
def stack_of_four():
    new_stack = Stack()
    new_stack.push('One')
    new_stack.push('Two')
    new_stack.push('Three')
    new_stack.push('Four')
    return new_stack
def stack_of_four():
    new_stack = Stack()
    new_stack.push('Penny')
    new_stack.push('Honey')
    new_stack.push('Lacey')
    new_stack.push('Pickles')
    return new_stack
Exemple #17
0
    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
Exemple #18
0
def test_many_push():
    new_stack = Stack()
    new_stack.push('One')
    new_stack.push('Two')
    new_stack.push('Three')
    new_stack.push('Four')
    assert new_stack.top.value == 'Four'
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_push_onto_full():
    s = Stack()
    s.push("apple")
    s.push("banana")
    s.push("cucumber")
    actual = s.top.value
    expected = "cucumber"
    assert actual == expected
def test_stack_pop_all_top():
    hats = Stack()
    hats.push('seahawks')
    hats.push('gray')
    hats.pop()
    hats.pop()

    assert hats.top is None
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
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
def test_stack_push():

    test_stack = Stack()
    assert test_stack.top == None
    test_stack.push(5)
    assert test_stack.top.value == 5
    test_stack.push('b')
    assert test_stack.top.value == 'b'
    test_stack.push('c')
    assert test_stack.top.value == 'c'
Exemple #25
0
def test_mult_pop_stack():
    stack = Stack()
    stack.push('1')
    stack.push('2')
    stack.pop()
    stack.pop()
    assert stack.is_empty() == True
Exemple #26
0
def test_peek_empty_exception():
    stack = Stack()
    stack.push(1)
    stack.pop()
    actual = stack.peek()
    expected = "empty stack"
    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():
    test_stack = Stack()
    test_stack.push(1)
    test_stack.push(2)
    test_stack.push(3)

    assert test_stack.peek() == 3
Exemple #30
0
def test_pop_one():
    stack = Stack()
    stack.push(1)
    stack.pop()
    expected = None
    actual = stack.peek()
    assert actual == expected