예제 #1
0
    def test_enqueue(self):
        queue = Queue()

        tests = [1, '0', Queue, lambda x: x, {}, [], None]

        for test in tests:
            queue.enqueue(test)

        assert len(tests) == len(queue)
예제 #2
0
    def test_dequeue_with_initial_queue(self):
        tests = [1, None, [], Queue, 'foo', lambda y: y, {}]
        queue = Queue(tests)

        assert len(queue) == len(tests)

        for _ in tests:
            queue.dequeue()

        assert queue.is_empty() is True
예제 #3
0
    def test_is_empty(self):
        queue = Queue()
        assert queue.is_empty() is True

        queue.enqueue(1)
        assert queue.is_empty() is False

        queue.dequeue()
        assert queue.is_empty() is True
예제 #4
0
    def traverse_level_order(self):
        queue = Queue()
        queue.enqueue(self)

        arr = []

        while (not queue.is_empty()):
            current = queue.dequeue()
            arr.append(current.data)
            for child in [current.left, current.right]:
                if (child):
                    queue.enqueue(child)

        return arr
예제 #5
0
    def test_size(self):
        queue = Queue()
        assert len(queue) == 0

        queue.enqueue(1)
        assert len(queue) == 1

        queue.dequeue()
        assert len(queue) == 0
예제 #6
0
    def test_queue_as_string(self):
        queue = Queue()

        assert str(queue) == ''

        queue.enqueue(3)
        queue.enqueue(1)
        queue.enqueue(2)

        assert str(queue) == '3 -> 1 -> 2'
예제 #7
0
def find_ladders(beginWord, endWord, wordList):
    """
    :type beginWord: str
    :type endWord: str
    :type wordList: List[str]
    :rtype: List[List[str]]
    """
    # ensure we add the beginWord to the graph
    graph_words = [beginWord] + wordList
    graph = Graph.from_same_length_word_list(graph_words)

    # to trace our path back when we find the target
    parent_of = defaultdict(lambda: [])
    depth = defaultdict(lambda: math.inf)
    depth[beginWord] = 0
    queue = Queue()
    queue.enqueue(beginWord)

    success = False
    while (not queue.is_empty()):
        current = queue.dequeue()
        if (current == endWord):
            success = True

        for child in graph.get_neighbors(current):
            not_encountered_yet = depth[child] == math.inf
            already_encountered_at_same_depth = depth[
                child] == depth[current] + 1

            if (not_encountered_yet):
                depth[child] = depth[current] + 1
                parent_of[child].append(current)
                queue.enqueue(child)
            elif (already_encountered_at_same_depth):
                parent_of[child].append(current)

    if success:
        all_paths = DoublyLinkedList()
        current_path = DoublyLinkedList()
        dfs(parent_of, endWord, all_paths, current_path)
        return all_paths.to_array_from_head()
    else:
        return []
예제 #8
0
    def test_dequeue(self):
        queue = Queue()

        with self.assertRaises(QueueDequeueException):
            queue.dequeue()

        one = 1
        two = 2

        queue.enqueue(one)
        queue.enqueue(two)

        assert len(queue) == 2

        assert queue.dequeue() == one
        assert queue.dequeue() == two

        assert len(queue) == 0
예제 #9
0
def step_impl(context):
    context.queue = Queue()
예제 #10
0
    def construct_from_list(self, list):
        """
            Creates a binary tree from a level-ordered list of values.
            Empty children for existing nodes should be specified with
            None.

            e.g. [4,7,3,None,None,1] yields a tree of shape:
                 4
                / \
               7   3
                  /
                 1
        """
        queue = Queue()
        root = self.__node_for_construction(list[0])
        current = root

        for data in list[1:]:
            new = self.__node_for_construction(data)
            if new is not None:
                queue.enqueue(new)

            if (current.left == self.__unassigned):
                current.left = new
            elif (current.right == self.__unassigned):
                current.right = new
            else:  # current node is full
                current = queue.dequeue()
                current.left = new

        # clean up (ensure unassigned values are represented as None)
        queue = Queue()
        queue.enqueue(root)
        while (not queue.is_empty()):
            current = queue.dequeue()
            if (current.left == self.__unassigned):
                current.left = None
            elif (current.left):
                queue.enqueue(current.left)

            if (current.right == self.__unassigned):
                current.right = None
            elif (current.right):
                queue.enqueue(current.right)

        return root
예제 #11
0
 def test_enqueue_one(self):
     queue = Queue(1)
     assert len(queue) == 1
     assert repr(queue) == '1'