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
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
Esempio n. 3
0
def test_empty_stack():
  """
  Exception raised when peeking empty stack.
  """

  stack = Stack()
  
  try:
    stack.peek()
  except EmptyStackException:
    assert True
Esempio n. 4
0
    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 test_stack_pop():
    testing_stack = Stack()
    testing_stack.push(1)
    testing_stack.push(2)
    expected = 2
    actual = testing_stack.peek()
    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
Esempio n. 7
0
def test_pop_one():
    stack = Stack()
    stack.push(1)
    stack.pop()
    expected = None
    actual = stack.peek()
    assert actual == expected
Esempio n. 8
0
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
Esempio n. 10
0
def test_peek_empty_exception():
    stack = Stack()
    stack.push(1)
    stack.pop()
    actual = stack.peek()
    expected = "empty stack"
    assert actual == expected
Esempio n. 11
0
def test_pop_off():
    stack = Stack()
    stack.push(1)
    stack.push(2)
    stack.pop()
    expected = 1
    actual = stack.peek()
    assert actual == expected
Esempio n. 12
0
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():
    # 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_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_peek():
    hats = Stack()
    hats.push('seahawks')
    hats.push('gray')
    hats.push('tan')
    hats.push('black')

    assert hats.peek() == 'black'
Esempio n. 16
0
def test_push_multiple():
    stack = Stack()
    stack.push('A')
    stack.push('B')
    stack.push('C')
    expected = 'C'
    actual = stack.peek()
    assert expected == actual
    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
Esempio n. 18
0
def test_peek_next_item():
    stack = Stack()
    stack.push(1)
    actual = stack.peek()
    expected = 1
    assert actual == expected
Esempio n. 19
0
def test_push_one():
    stack = Stack()
    stack.push(1)
    expected = 1
    actual = stack.peek()
    assert actual == expected
def test_stack_peek():
    testing_stack = Stack()
    testing_stack.push('hello')
    expected = 'hello'
    actual = testing_stack.peek()
    assert actual == expected
Esempio n. 21
0
def test_peek_stack():
    stack = Stack()
    stack.push('1')
    stack.push('2')
    assert stack.peek() == '2'
def test_peek_stack():
    stack = Stack()
    stack.push('works')
    assert stack.peek() == 'works'
def test_stack_peek_empty():
    hats = Stack()

    assert hats.peek() == 'Empty Stack'
Esempio n. 24
0
def test_peek():
    element = Stack()
    element.push(1)
    assert element.peek() == 1
def test_peek_empty():
    s = Stack()
    with pytest.raises(InvalidOperationError) as e:
        s.peek()

    assert str(e.value) == "Method not allowed on empty collection"
Esempio n. 26
0
def test_exceptions_():
    with pytest.raises(Exception):
        stack = Stack()
        stack.pop()
        stack.peek()
Esempio n. 27
0
def test_Stack_empty():
    stack = Stack()
    actual = stack.peek()
    expected = None
    assert actual == expected
Esempio n. 28
0
def test_stack_peek():
    s = Stack()
    s.push('1')
    s.push('2')

    assert s.peek() == '2'
def test_none_peek():
    new_stack = Stack()
    assert new_stack.peek() == None