def test_queue_remove(): queue = Queue() queue.add(1) queue.add(2) queue.add(3) assert queue.remove() == 1 assert queue.remove() == 2 assert queue.remove() == 3 assert queue.remove() == None
def test_queue_peek(): queue = Queue() queue.add(1) queue.add(2) queue.add(3) assert queue.peek() == 1 assert queue.first.data == 1
def test_queue_is_empty(): queue = Queue() assert queue.is_empty() queue.add(1) assert not queue.is_empty()
def test_queue_add(): queue = Queue() queue.add(1) assert queue.first is not None assert queue.last is not None assert queue.first.data == 1 assert queue.first is queue.last queue.add(2) assert queue.first.data == 1 assert queue.last.data == 2
def __init__(self): self.cats = Queue() self.dogs = Queue() self.counter = 0
class PetManager: def __init__(self): self.cats = Queue() self.dogs = Queue() self.counter = 0 def enqueue(self, animal: str): assert animal == "cat" or animal == "dog" if animal == "cat": self.cats.add(("cat", self.counter)) else: self.dogs.add(("dog", self.counter)) self.counter += 1 def dequeue_cat(self) -> (str, int) or None: return self.cats.remove() def dequeue_dog(self) -> (str, int) or None: return self.dogs.remove() def dequeue_any(self) -> (str, int) or None: if self.cats.is_empty(): return self.dogs.remove() if self.dogs.is_empty(): return self.cats.remove() _, cat_n = self.cats.peek() _, dog_n = self.dogs.peek() if cat_n < dog_n: return self.cats.remove() else: return self.dogs.remove()
def test_queue_init(): queue = Queue() assert queue.first is None assert queue.last is None