def test_peek_from_queue(): queue = Queue() queue.enqueue(1) queue.enqueue(2) assert queue.peek() == 1 assert queue.dequeue() == 1 assert queue.peek() == 2 assert queue.dequeue() == 2 assert queue.peek() == 'Queue is empty'
def test_is_empty(): queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.dequeue() queue.dequeue() assert queue.isEmpty() == True
class AnimalShelter(): def __init__(self): self.dogs = Queue() self.cats = Queue() def enqueue(self, animal): try: if animal.type == 'cat': self.cats.enqueue(animal) elif animal.type == 'dog': self.dogs.enqueue(animal) except AttributeError as e: return 'Add Cat or Dog Only' def dequeue(self, pref): try: if pref == 'cat': cat = self.cats.dequeue() return cat.name elif pref == 'dog': dog = self.dogs.dequeue() return dog.name except AttributeError as e: return None
def bfs(self, start_node): nodes = [] visited = set() breadth = Queue() breadth.enqueue(start_node) visited.add(start_node) while len(breadth)>0: node = breadth.dequeue() nodes.append(node) for n in self.adjacency_list[node]: if n not in visited: breadth.enqueue(n) visited.add(n) return nodes
def test_peek_on_empty_queue(): nums = Queue() actual = nums.peek() expected = 'Queue is empty' assert actual == expected
def test_init(): queue = Queue() assert queue.front == None assert queue.rear == None
def test_queue_peek(): queue = Queue() queue.enqueue(7) queue.enqueue(3) assert queue.peek() == 7
def test_dequeue(): queue = Queue() queue.enqueue(1) queue.enqueue(3) queue.dequeue() assert queue.front.value == 3
def test_enqueue_multiple(): queue = Queue() queue.enqueue(1) queue.enqueue(2) assert queue.rear.value == 2 assert queue.front.value == 1
def test_enqueue_one(): queue = Queue() queue.enqueue(6) assert queue.rear.value == 6
def __init__(self): self.dogs = Queue() self.cats = Queue()