def test_dequeue():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("orange")
    actual = q.dequeue()
    expected = "apple"
    assert actual == expected
Exemplo n.º 2
0
def test_enqueue_two():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    actual = q.peek()
    expected = "apples"
    assert actual == expected
Exemplo n.º 3
0
def test_add_empty_to_filled_queue():
    q1 = Queue()
    q1.enqueue('cat')
    q2 = Queue()
    q1 + q2
    assert q1.front.value == 'cat'
    assert q1.rear.value == 'cat'
Exemplo n.º 4
0
def test_dequeue_when_full():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    actual = q.dequeue()
    expected = "apples"
    assert actual == expected
Exemplo n.º 5
0
def test_exhausted():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.dequeue()
    q.dequeue()
    assert q.is_empty() == True
Exemplo n.º 6
0
def test_add_two_queues():
    q1 = Queue()
    q1.enqueue('cat')
    q2 = Queue()
    q2.enqueue('dog')
    q1 + q2
    assert q1.front.value == 'cat'
    assert q1.rear.value == 'dog'
Exemplo n.º 7
0
def test_dequeue():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.dequeue()
    actual = q.peek()
    expected = "banana"
    assert actual == expected
def test_exhausted():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("orange")
    q.dequeue()
    q.dequeue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
Exemplo n.º 9
0
    def dequeue(self, preference=None):
        if not preference:
            return super().dequeue()

        current = super().dequeue()
        q = Queue()
        while (current != preference):
            print(self)
            q.enqueue(current)
            current = super().dequeue()
        if (self.front):
            self.front.next = q.rear
        self.front = q.front
        return current
    def breadth_first(self, node):
        visited = [node]

        q = Queue()
        q.enqueue(node)

        while not q.is_empty():
            node = q.dequeue()

            neighbors = [edge.vertex for edge in self.get_neighbors(node)]

            for neighbor in neighbors:
                if neighbor not in visited:
                    visited.append(neighbor)
                    q.enqueue(neighbor)

        return visited
Exemplo n.º 11
0
def test_dequeue():
    queue = Queue()
    queue.enqueue('Dennis')
    queue.enqueue('Mac')
    queue.enqueue('Charlie')
    queue.enqueue('Dee')
    queue.dequeue()
    actual = queue.front.value
    expected = 'Mac'
    assert actual == expected
Exemplo n.º 12
0
def test_dequeue_empty():
    queue = Queue()
    queue.enqueue('Dennis')
    queue.enqueue('Mac')
    queue.enqueue('Charlie')
    queue.enqueue('Dee')
    queue.dequeue()
    queue.dequeue()
    queue.dequeue()
    queue.dequeue()
    actual = queue.is_empty()
    expected = True
    assert actual == expected
class AnimalShelter():
    def __init__(self):
        self.cats = Queue()
        self.dogs = Queue()

    def enqueue(self, animal: str) -> str:
        if animal.type == "cat":
            self.cats.enqueue(animal)
        elif animal.type == "dog":
            self.dogs.enqueue(animal)
        else:
            raise Exception("Animal type provided is not valid")

    def dequeue(self, preference: str) -> str:
        if preference == "cat":
            return self.cats.dequeue()
        elif preference == "dog":
            return self.dogs.dequeue()
        else:
            raise Exception("Unable to successfully dequeue")
class AnimalShelter(Queue):
    """Creates an Instance of AnimalShelter as a sub-class of Queue
  """
    def __init__(self):
        self.dog = Queue()
        self.cat = Queue()
        self.cat_wait = 0
        self.dog_wait = 0

    def enqueue(self, animal: object):
        """adds an animal to AnimalShelter instance using the species attr. of animal object. Distinct from the enqueue method of SuperClass: Queue.
    input <-- object
    output --> mutates Queue in place. non-fruitful
    """
        #use property species to sort inputs into respective sub-queues
        if animal.species == 'dog':
            self.dog.enqueue(animal)
        elif animal.species == 'cat':
            self.cat.enqueue(animal)
        else:
            return f'We\'re sorry, we cannot take {animal.species}s at this shelter'

    def dequeue(self, pref: str = 'cat') -> str:
        """Returns the name of first animal species equal to pref. defaults to cat
    input <-- str
    output --> str
    """

        if pref == 'dog':
            adopted = self.dog.dequeue()
            self.cat_wait += 1
            return adopted.name
        elif pref == 'cat':  # explicitly checks default to allow exception if pref not available at shelter.
            adopted = self.cat.dequeue()
            self.dog_wait += 1
            return adopted.name
        else:
            #uses f-string for user facing error. backend Exception Handling could also be implemented.
            return f'We\'re sorry, we have no {pref}s available for adoption'
Exemplo n.º 15
0
def test_peek():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.enqueue("cucumber")
    actual = q.peek()
    expected = "apple"
    assert actual == expected
Exemplo n.º 16
0
def test_enqueue_multiple():
    queue = Queue()
    queue.enqueue('Ron')
    queue.enqueue('Andy')
    queue.enqueue('Leslie')
    actual = queue.rear.value
    expected = 'Leslie'
    assert actual == expected
def test_enqueue_onto_full_queue():
    s = Queue()
    s.enqueue("apple")
    s.enqueue("cucumber")
    s.enqueue("phone")
    actual = s.rear.value
    excepted = "phone"
    assert actual == excepted
def test_peek_queue_long():
    s = Queue()
    s.enqueue("apple")
    s.enqueue("cucumber")
    s.enqueue("phone")
    actual = s.peek()
    expected = "apple"
    assert actual == expected
Exemplo n.º 19
0
def test_exhausted():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.enqueue("cucumber")
    q.dequeue()
    q.dequeue()
    q.dequeue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
def test_queue_enqueu():
    test_queue = Queue()
    assert test_queue.front == None
    test_queue.enqueue(5)
    assert test_queue.front.value == 5
    test_queue.enqueue('b')
    assert test_queue.rear.value == 'b'
    assert test_queue.front.value == 5
    test_queue.enqueue('c')
    assert test_queue.rear.value == 'c'
    assert test_queue.front.value == 5
Exemplo n.º 21
0
 def breadth_first(self) -> list:
     breadth_queue = Queue()
     output = []
     breadth_queue.enqueue(self.root)
     while not breadth_queue.is_empty():
         front = breadth_queue.dequeue()
         output.append(front.value)
         if front.left:
             breadth_queue.enqueue(front.left)
         if front.right:
             breadth_queue.enqueue(front.right)
     return output
Exemplo n.º 22
0
    def breadth_traverse(self):
        if not self.root:
            raise Exception('tree is empty')

        output = []
        q = Queue()
        q.enqueue(self.root)
        while not q.is_empty():
            current = q.dequeue()
            if current.left:
                q.enqueue(current.left)
            if current.right:
                q.enqueue(current.right)
            output.append(current.value)
        return output
Exemplo n.º 23
0
def test_enqueue():
    q = Queue()
    q.enqueue("apple")
    actual = q.front.value
    expected = "apple"
    assert actual == expected
def test_peek_queue():
    s = Queue()
    s.enqueue("apple")
    actual = s.peek()
    expected = "apple"
    assert actual == expected
Exemplo n.º 25
0
def test_enqueue():
    queue = Queue()
    queue.enqueue('Ron')
    actual = queue.rear.value
    expected = 'Ron'
    assert actual == expected
def test_queue():
    test_queue = Queue()
    nodes = ['a', 'b', 'c', 'd']
    for el in nodes:
        test_queue.enqueue(el)
    return test_queue
def test_enqueue_onto_empty_queue():
    s = Queue()
    s.enqueue("apple")
    actual = s.rear.value
    excepted = "apple"
    assert actual == excepted
def test_dequeue_queue():
    s = Queue()
    s.enqueue("apple")
    actual = s.dequeue()
    expected = "apple"
    assert actual == expected
Exemplo n.º 29
0
def test_shelter_with_a_cat():
    q = Queue()
    q.enqueue('cat')
    assert q.front.value == 'cat'
    assert q.rear.value == 'cat'