コード例 #1
0
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'
コード例 #2
0
def test_is_empty():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    queue.dequeue()
    queue.dequeue()
    assert queue.isEmpty() == True
コード例 #3
0
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
コード例 #4
0
 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
コード例 #5
0
def test_peek_on_empty_queue():
    nums = Queue()
    actual = nums.peek()
    expected = 'Queue is empty'
    assert actual == expected
コード例 #6
0
def test_init():
    queue = Queue()
    assert queue.front == None
    assert queue.rear == None
コード例 #7
0
def test_queue_peek():
    queue = Queue()
    queue.enqueue(7)
    queue.enqueue(3)
    assert queue.peek() == 7
コード例 #8
0
def test_dequeue():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(3)
    queue.dequeue()
    assert queue.front.value == 3
コード例 #9
0
def test_enqueue_multiple():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    assert queue.rear.value == 2
    assert queue.front.value == 1
コード例 #10
0
def test_enqueue_one():
    queue = Queue()
    queue.enqueue(6)
    assert queue.rear.value == 6
コード例 #11
0
 def __init__(self):
     self.dogs = Queue()
     self.cats = Queue()