Пример #1
0
class PseudoQueue:
    """Queue data structure made with Stack data structure under the hood."""
    def __init__(self):
        """PseudoQueue constructor."""

        self.stack_one = Stack()
        self.stack_two = Stack()
        self.size = 0

    def enqueue(self, value):
        """Add a value to the rear of the pseudo queue."""

        for _ in range(self.size):
            node_value = self.stack_one.pop()
            self.stack_two.push(node_value)

        self.stack_two.push(value)
        self.size += 1

        for _ in range(self.size):
            node_value = self.stack_two.pop()
            self.stack_one.push(node_value)

    def dequeue(self):
        """Remove a value from the front of the pseudo queue."""

        if not self.size:
            return EmptyQueueException()

        return self.stack_one.pop()

    def peek(self):
        """See the pseudo front queue value."""

        return self.stack_one.peek()
Пример #2
0
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
Пример #3
0
def test_peek():
    s = Stack()
    s.push("apple")
    s.push("banana")
    actual = s.peek()
    expected = "banana"
    assert actual == expected
Пример #4
0
def test_check_not_empty():
    s = Stack()
    s.push("apple")
    s.push("banana")
    actual = s.is_empty()
    expected = False
    assert actual == expected
Пример #5
0
    def __init__(self):
        """AnimalShelter constructor."""

        self.main_stack = Stack()
        self.helper_stack = Stack()
Пример #6
0
class AnimalShelter:
    """Queue of animals in a shelter."""

    def __init__(self):
        """AnimalShelter constructor."""

        self.main_stack = Stack()
        self.helper_stack = Stack()

    def enqueue(self, animal):
        """
        Bring an animal into the shelter.

        Parameters:
        animal (object): The Cat or Dog instance to bring into the shelter.
        """

        while not isinstance(self.main_stack.peek(), EmptyStackException):
            self.helper_stack.push(self.main_stack.pop())

        self.helper_stack.push(animal)

        while not isinstance(self.helper_stack.peek(), EmptyStackException):
            self.main_stack.push(self.helper_stack.pop())

    def dequeue(self, animal):
        """
        Take an animal out of the shelter.

        Parameters:
        animal (class): The Cat or Dog instance which should be taken out of the shelter.

        Returns:
        (Animal): The instance of the Cat or Dog class first in the queue in the shelter.
        """

        if isinstance(self.main_stack.peek(), EmptyStackException):
            return EmptyStackException()

        saved_animal = None

        while not isinstance(self.main_stack.peek(), EmptyStackException):
            if not saved_animal and isinstance(self.main_stack.peek(), animal):
                saved_animal = self.main_stack.pop()
            else:
                self.helper_stack.push(self.main_stack.pop())

        while not isinstance(self.helper_stack.peek(), EmptyStackException):
            self.main_stack.push(self.helper_stack.pop())

        return saved_animal

    def peek(self):
        """See the first animal in the shelter."""

        return self.main_stack.peek()
Пример #7
0
def test_pop_empty():
    s = Stack()
    with pytest.raises(InvalidOperationError) as e:
        s.pop()

    assert str(e.value) == "Method not allowed on empty collection"
Пример #8
0
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
Пример #9
0
def test_push_onto_empty():
    s = Stack()
    s.push("apple")
    actual = s.top.value
    expected = "apple"
    assert actual == expected
Пример #10
0
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
Пример #11
0
def test_pop_single():
    s = Stack()
    s.push("apple")
    actual = s.pop()
    expected = "apple"
    assert actual == expected
Пример #12
0
    def __init__(self):
        """PseudoQueue constructor."""

        self.stack_one = Stack()
        self.stack_two = Stack()
        self.size = 0