Exemple #1
0
    def test_mergeable_heap_on_sorted_list(self):
        _, elements1 = get_random_unique_array(min_size=0)
        _, elements2 = get_random_unique_array(min_size=0)

        heap1 = sorted_list_make_min_heap()
        heap2 = sorted_list_make_min_heap()

        for element in elements1:
            sorted_list_min_heap_insert(heap1, element)
        for element in elements2:
            sorted_list_min_heap_insert(heap2, element)
        assert_sorted_list(heap1)
        assert_sorted_list(heap2)

        expected_elements = elements1 + elements2

        if elements1:
            actual_min = sorted_list_heap_minimum(heap1)
            actual_extracted_min = sorted_list_heap_extract_min(heap1)
            expected_min = min(elements1)
            expected_elements.remove(expected_min)
            assert_that(actual_min, is_(equal_to(expected_min)))
            assert_that(actual_extracted_min, is_(equal_to(expected_min)))
            assert_sorted_list(heap1)
        else:
            assert_that(
                calling(sorted_list_heap_extract_min).with_args(heap1),
                raises(RuntimeError, 'heap underflow'))

        if elements2:
            actual_min = sorted_list_heap_minimum(heap2)
            actual_extracted_min = sorted_list_heap_extract_min(heap2)
            expected_min = min(elements2)
            expected_elements.remove(expected_min)
            assert_that(actual_min, is_(equal_to(expected_min)))
            assert_that(actual_extracted_min, is_(equal_to(expected_min)))
            assert_sorted_list(heap2)
        else:
            assert_that(
                calling(sorted_list_heap_extract_min).with_args(heap2),
                raises(RuntimeError, 'heap underflow'))

        merged_heap = sorted_list_min_heap_union(heap1, heap2)

        actual_elements = get_linked_list_keys(merged_heap)
        expected_elements = list(sorted(set(expected_elements)))
        assert_that(actual_elements, is_(equal_to(expected_elements)))
        assert_sorted_list(merged_heap)
    def test_min_gap_tree(self):
        _, keys = get_random_unique_array()
        tree = RedBlackTree()
        tree.nil.min_key = tree.nil.min_gap = math.inf
        tree.nil.max_key = -math.inf

        for key in keys:
            min_gap_insert(tree, Node(key))

        nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)

        while nodes:
            node = random.choice(nodes)
            key = node.key
            keys.remove(key)

            actual_found = min_gap_search(tree, key)
            assert_that(actual_found.key, is_(equal_to(key)))

            min_gap_delete(tree, node)

            actual_found = min_gap_search(tree, key)
            assert_that(actual_found, is_(tree.nil))

            actual_min_gap = min_gap(tree)

            expected_min_gap = get_expected_min_gap(keys)
            assert_that(actual_min_gap, is_(equal_to(expected_min_gap)))
            nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
Exemple #3
0
def get_random_red_black_tree(black_height=3, min_value=0, max_value=999, sentinel=rb.Node(None)):
    nodes = []
    tree = RedBlackTree(get_random_red_black_subtree(black_height, nodes), sentinel=sentinel)
    tree_size = len(nodes)
    _, keys = get_random_unique_array(min_size=tree_size, max_size=tree_size, min_value=min_value, max_value=max_value)
    keys.sort()
    fill_subtree_with_keys(tree.root, keys, tree.nil)
    return tree, nodes, keys
Exemple #4
0
    def test_small_order_select(self):
        array, elements = get_random_unique_array(max_size=50)
        i = random.randint(1, array.length // 5 + 1)  # pick small i

        actual_order_statistic = small_order_select(array, i)

        expected_order_statistic = sorted(elements)[i - 1]
        assert_that(actual_order_statistic, is_(equal_to(expected_order_statistic)))
    def test_median_neighbors(self):
        array, elements = get_random_unique_array()
        k = random.randint(1, array.length)

        actual_neighbors = median_neighbors(array, k)

        expected_neighbors = get_expected_neighbors(elements, k)
        assert_that(expected_neighbors, is_(equal_to(actual_neighbors)))
Exemple #6
0
    def test_sum_search(self):
        array, elements = get_random_unique_array(max_value=20)
        sum_to_find = random.randint(0, 40)

        actual_found = sum_search(array, sum_to_find)

        all_sums = {x + y for x in elements for y in elements if y != x}
        expected_found = sum_to_find in all_sums
        assert_that(actual_found, is_(equal_to(expected_found)))
    def test_sum_search(self):
        array, elements = get_random_unique_array(max_value=20)
        sum_to_find = random.randint(0, 40)

        actual_found = sum_search(array, sum_to_find)

        all_sums = {x + y for x in elements for y in elements if y != x}
        expected_found = sum_to_find in all_sums
        assert_that(actual_found, is_(equal_to(expected_found)))
    def test_quantiles(self):
        array, elements = get_random_unique_array()
        k = random.randint(1, array.length + 1)

        actual_quantiles = quantiles(array, 1, array.length, k)

        assert_that(actual_quantiles, has_length(k - 1))
        if k > 1:
            assert_quantiles(actual_quantiles, elements)
    def test_perfect_hashing(self):
        keys, _ = get_random_unique_array(max_value=99)

        table, h = perfect_hashing_init(keys)

        for key in range(100):
            actual_found = perfect_hashing_search(table, key, h)
            if key in keys:
                assert_that(actual_found, is_(not_none()))
                j, j_ = actual_found
                assert_that(table[j][1][j_], is_(equal_to(key)))
            else:
                assert_that(actual_found, is_(none()))
Exemple #10
0
def get_random_red_black_tree(black_height=3,
                              min_value=0,
                              max_value=999,
                              sentinel=rb.Node(None)):
    nodes = []
    tree = RedBlackTree(get_random_red_black_subtree(black_height, nodes),
                        sentinel=sentinel)
    tree_size = len(nodes)
    _, keys = get_random_unique_array(min_size=tree_size,
                                      max_size=tree_size,
                                      min_value=min_value,
                                      max_value=max_value)
    keys.sort()
    fill_subtree_with_keys(tree.root, keys, tree.nil)
    return tree, nodes, keys
Exemple #11
0
    def test_make_change(self):
        n = random.randint(1, 20)
        k = random.randint(1, 5)
        d, _ = get_random_unique_array(max_size=k, min_value=2, max_value=20)
        d[1] = 1
        captured_output = io.StringIO()

        actual_change, actual_denominators = make_change(n, d)
        with redirect_stdout(captured_output):
            print_change(n, actual_denominators)

        expected_change_size = get_min_change_size_bruteforce(n, d)
        assert_that(actual_change[n], is_(equal_to(expected_change_size)))
        actual_change_denoms = [int(d) for d in captured_output.getvalue().splitlines()]
        assert_that(sum(actual_change_denoms), is_(equal_to(n)))
        assert_that(len(actual_change_denoms), is_(equal_to(expected_change_size)))
Exemple #12
0
    def test_list_min_heap_disjoint_union(self):
        array, elements = get_random_unique_array(min_size=0)
        heap1_size = random.randint(0, len(elements))
        elements1 = array[1:heap1_size].elements
        elements2 = array[heap1_size + 1:array.length].elements

        heap1 = list_make_min_heap()
        heap2 = list_make_min_heap()

        for element in elements1:
            list_min_heap_insert(heap1, element)
        for element in elements2:
            list_min_heap_insert(heap2, element)

        merged_heap = list_min_heap_disjoint_union(heap1, heap2)

        actual_elements = get_linked_list_keys(merged_heap)
        assert_that(actual_elements, contains_inanyorder(*elements))
    def test_median_nearest(self):
        array, elements = get_random_unique_array(max_value=30)
        k = random.randint(1, array.length)

        actual_nearest = median_nearest(array, k)

        median_index = (len(elements) - 1) // 2
        median = sorted(elements)[median_index]
        distances = [(x, abs(x - median)) for x in elements]
        sorted_distances = sorted(distances, key=lambda p: p[1])
        expected_nearest = {p[0] for p in sorted_distances[:k]}
        # also add (k+1)-th number if its distance to median is equal to one of k elements already taken
        if k < len(elements) and sorted_distances[k][1] == sorted_distances[
                k - 1][1]:
            expected_nearest |= {sorted_distances[k][0]}

        assert_that(actual_nearest, has_length(k))
        assert_that(actual_nearest.issubset(expected_nearest))
Exemple #14
0
    def test_make_change(self):
        n = random.randint(1, 20)
        k = random.randint(1, 5)
        d, _ = get_random_unique_array(max_size=k, min_value=2, max_value=20)
        d[1] = 1
        captured_output = io.StringIO()

        actual_change, actual_denominators = make_change(n, d)
        with redirect_stdout(captured_output):
            print_change(n, actual_denominators)

        expected_change_size = get_min_change_size_bruteforce(n, d)
        assert_that(actual_change[n], is_(equal_to(expected_change_size)))
        actual_change_denoms = [
            int(d) for d in captured_output.getvalue().splitlines()
        ]
        assert_that(sum(actual_change_denoms), is_(equal_to(n)))
        assert_that(len(actual_change_denoms),
                    is_(equal_to(expected_change_size)))
    def test_in_place_chained_hash_table(self):
        table_size = random.randint(1, 20)
        keys, _ = get_random_unique_array(max_size=table_size, max_value=99)
        elements = [Element(key) for key in keys]
        table = Array.indexed(0, table_size - 1)
        table.free = 0
        for i in range(table_size):
            table[i] = FreePosition(i - 1, i + 1)
        table[table_size - 1].next = -1
        h = lambda k, m: k % m

        for element in elements:
            in_place_chained_hash_insert(table, element, h)

        for key in range(100):
            actual_element = in_place_chained_hash_search(table, key, h)

            if key in keys:
                assert_that(actual_element.key, is_(equal_to(key)))
                in_place_chained_hash_delete(table, actual_element, h)
            else:
                assert_that(actual_element, is_(none()))

        assert_in_place_hash_table_clear(table)
Exemple #16
0
    def test_in_place_chained_hash_table(self):
        table_size = random.randint(1, 20)
        keys, _ = get_random_unique_array(max_size=table_size, max_value=99)
        elements = [Element(key) for key in keys]
        table = Array.indexed(0, table_size - 1)
        table.free = 0
        for i in range(table_size):
            table[i] = FreePosition(i - 1, i + 1)
        table[table_size - 1].next = -1
        h = lambda k, m: k % m

        for element in elements:
            in_place_chained_hash_insert(table, element, h)

        for key in range(100):
            actual_element = in_place_chained_hash_search(table, key, h)

            if key in keys:
                assert_that(actual_element.key, is_(equal_to(key)))
                in_place_chained_hash_delete(table, actual_element, h)
            else:
                assert_that(actual_element, is_(none()))

        assert_in_place_hash_table_clear(table)