def test_exhausted(): q = Queue() q.enqueue("apple") q.enqueue("banana") q.dequeue() q.dequeue() assert q.is_empty() == True
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
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
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_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
def test_dequeue_when_full(): q = Queue() q.enqueue("apples") q.enqueue("bananas") actual = q.dequeue() expected = "apples" assert actual == expected
def test_dequeue(): q = Queue() q.enqueue("apple") q.enqueue("orange") actual = q.dequeue() expected = "apple" 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")
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
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'
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
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
def test_call_dequeue_on_empty_queue(): new_queue = Queue() with pytest.raises(InvalidOperationError) as e: new_queue.dequeue() assert str(e.value) == "Method not allowed on empty collection"
def test_dequeue_when_empty(): q = Queue() with pytest.raises(InvalidOperationError) as e: q.dequeue() assert str(e.value) == "Method not allowed on an empty collection"
def test_dequeue_queue(): s = Queue() s.enqueue("apple") actual = s.dequeue() expected = "apple" assert actual == expected
def test_dequeue_when_empty(): q = Queue() with pytest.raises(InvalidOperationError): q.dequeue()