def test_dequeue_one(): queue = Queue() queue.enqueue(2) queue.enqueue(1) actual = queue.dequeue() expected = 2 assert actual == expected
def breadth_first(self, start: Vertex) -> set: """Traverse a graph in breadth-first manner and return a set of Vertices Args: start (Vertex): Starting point of the traversal Raises: KeyError: If provided vertex is not part of the graph Returns: set: Set of vertices """ q = Queue() output = set() if not start in self.adjacency_list: raise KeyError('Given Vertex is not part of the Graph') q.enqueue(start) while not q.is_empty(): vertex = q.dequeue() output.add(vertex) for neighbor in self.get_neighbors(vertex): if neighbor['vertex'] not in output: q.enqueue(neighbor['vertex']) return output
def test_dequeue(): q = Queue() q.enqueue('apple') q.dequeue() actual = q.is_empty() expected = True assert actual == expected
def test_queue_enqueue(): value = 1 queue = Queue() queue.enqueue(value) expected = value actual = queue.front.value assert actual == expected
def test_enqueue_two(): q = Queue() q.enqueue("apples") q.enqueue("bananas") actual = q.peek() expected = "apples" 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_queue_dequeue(): value = 1 queue = Queue() queue.enqueue(value) actual = queue.dequeue() expected = value assert actual == expected
def test_can_peek_queue(): queue = Queue() queue.enqueue(1) queue.enqueue(2) actual = queue.peek() expected = 1 assert actual == expected
def test_enqueue_multiple(): queue = Queue() queue.enqueue(3) queue.enqueue(2) queue.enqueue(1) actual = queue.rear.value expected = 1 assert actual == expected
def test_queue_enqueue_many(): value1 = 1 value2 = 2 queue = Queue() queue.enqueue(value1) queue.enqueue(value2) expected = value1 actual = queue.front.value assert actual == expected
def test_can_empty_queue(): queue = Queue() queue.enqueue(1) queue.enqueue(1) queue.dequeue() queue.dequeue() actual = queue.is_empty() expected = True assert actual == expected
def test_queue_dequeue_until_empty(): value1 = 1 value2 = 2 queue = Queue() queue.enqueue(value1) queue.enqueue(value2) queue.dequeue() queue.dequeue() actual = queue.front expected = None assert actual == expected
def test_exhausted(): # TODO TODO is empty should return true after dequeueing all previously queued items q = Queue() q.enqueue('apples') q.enqueue('oranges') q.dequeue() q.dequeue() actual = q.is_empty() expected = True assert actual == expected
def test_peek_post_dequeue(): # TODO it should return a new first value if peek is called after a dequeue q = Queue() q.enqueue('apples') q.enqueue('oranges') actual = q.peek() expected = 'apples' assert actual == expected q.dequeue() actual = q.peek() expected = 'oranges' assert actual == expected
def breadth_first(self, vertex): node = [] breadth = Queue() breadth.enqueue(vertex) visited = set() while not breadth.is_empty(): front = breadth.dequeue() node.append(front.value) for neighbor in self.get_neighbor(front): if neighbor.vertex.value not in visited: visited.add(neighbor.vertex.value) breadth.enqueue(neighbor.vertex) return node
class AnimalShelter: def __init__(self): self.cats = Queue() self.dogs = Queue() self.animal_order = Queue() def enqueue(self, animal): """Add passed in animal object to the queue Args: animal (obj): Expects either Cat() or Dog() instance Raises: ValueError: If not Cat() or Dog() is passed in """ if animal.type == 'Cat': self.cats.enqueue(animal) self.animal_order.enqueue('Cat') elif animal.type == 'Dog': self.dogs.enqueue(animal) self.animal_order.enqueue('Dog') else: raise ValueError('Must be either a cat or a dog!') def dequeue(self, preference=None): """Get the longest waiting in queue Cat() or Dog() depending on passed in preference. If no preference passed in get the longest waiting in queue animal Args: preference (str, optional): Animal to be returned (cat or dog). Defaults to None. Raises: ValueError: If not a cat or a dog is passed in as a preference AttributeError: When no animals is available Returns: obj: Cat() or Dog() instance """ try: if not preference: preference = self.animal_order.dequeue() if preference.lower() == 'cat': animal = self.cats.dequeue() elif preference.lower() == 'dog': animal = self.dogs.dequeue() else: raise ValueError('Must be either a cat or a dog!') return animal except AttributeError as err: raise AttributeError('No animals available!')
def add(self, val: any) -> None: """Add a new node with Breadth first approach Args: val (any): Value to add """ new_node = Node(val) if self.root: q = Queue() q.enqueue(self.root) while not q.is_empty(): node = q.dequeue() if node.left: q.enqueue(node.left) else: node.left = new_node return if node.right: q.enqueue(node.right) else: node.right = new_node return else: self.root = new_node
def test_peek(): q = Queue() q.enqueue("apple") q.enqueue("banana") q.enqueue("cucumber") actual = q.peek() expected = "apple" assert actual == expected
def breadth_first(self) -> list: """Traverse the tree in breadth-first manner Returns: list: Values of the tree """ output = [] if self.root: q = Queue() q.enqueue(self.root) while not q.is_empty(): el = q.dequeue() output.append(el.val) if el.left: q.enqueue(el.left) if el.right: q.enqueue(el.right) return output
def test_enqueue_into_queue(): queue = Queue() queue.enqueue(1) actual = queue.rear.value expected = 1 assert actual == expected
def test_multiple_enqueue_fails(): queue = Queue() queue.enqueue(3) queue.enqueue(2) queue.enqueue(1) assert queue.rear.value != 2
def test_enqueue(): q = Queue() q.enqueue("apple") actual = q.front.value expected = "apple" assert actual == expected