def test_bitonic_tsp(self): n = random.randint(3, 12) xcoords, _ = get_random_array(min_size=n, max_size=n) ycoords, _ = get_random_array(min_size=n, max_size=n) points = Array([Point2D(x, y) for x, y in zip(xcoords, ycoords)]) captured_output = io.StringIO() actual_path_lengths, optimal_paths = bitonic_tsp(points) with redirect_stdout(captured_output): print_bitonic_path(points, optimal_paths) expected_bitonic_path_length = get_shortest_bitonic_path_length_bruteforce( points) assert_that(actual_path_lengths[n, n], is_(close_to(expected_bitonic_path_length, 1e-7))) pattern = re.compile('\((\d+), (\d+)\)') actual_bitonic_path = [ Point2D(int(pattern.match(point).group(1)), int(pattern.match(point).group(2))) for point in captured_output.getvalue().splitlines() ] assert_that(actual_bitonic_path, has_length(n)) path_length_from_bitonic_path = get_path_length_from_bitonic_path( actual_bitonic_path) assert_that(path_length_from_bitonic_path, is_(close_to(expected_bitonic_path_length, 1e-7)))
def test_jobs_scheduling(self): n = random.randint(1, 8) times, times_elements = get_random_array(min_size=n, max_size=n, min_value=1, max_value=n) profits, profits_elements = get_random_array(min_size=n, max_size=n) deadlines_elements = [ random.randint(times_elements[j], n**2 + 10) for j in range(n) ] deadlines = Array(deadlines_elements) captured_output = io.StringIO() actual_max_profits, actual_schedule, sorted_job_ids = jobs_scheduling( times, profits, deadlines) with redirect_stdout(captured_output): print_schedule(actual_schedule, sorted_job_ids, times, deadlines, n, actual_schedule[0].length - 1) expected_max_profit = get_optimal_schedule_bruteforce( Array(times_elements), Array(profits_elements), Array(deadlines_elements)) assert_that(actual_max_profits[actual_max_profits.length - 1], is_(equal_to(expected_max_profit))) scheduled_jobs = [ int(re.search('a(\d+)', job).group(1)) for job in captured_output.getvalue().splitlines() ] profit_from_schedule = get_schedule_total_profit( scheduled_jobs, Array(times_elements), Array(profits_elements), Array(deadlines_elements)) assert_that(profit_from_schedule, is_(equal_to(expected_max_profit)))
def test_sets_reordering(self): n = random.randint(1, 8) bases, _ = get_random_array(min_size=n, max_size=n, min_value=1, max_value=10) exponents, _ = get_random_array(min_size=n, max_size=n, min_value=1, max_value=10) sets_reordering(bases, exponents) actual_score = get_score(bases, exponents) expected_score = sets_reordering_bruteforce(bases, exponents) assert_that(actual_score, is_(equal_to(expected_score)))
def test_tasks_scheduling_(self): n = random.randint(1, 8) deadlines, _ = get_random_array(min_size=n, max_size=n, min_value=1, max_value=n) penalties, _ = get_random_array(min_size=n, max_size=n) actual_schedule = tasks_scheduling_(deadlines, penalties) schedule_ids = decode_tasks(actual_schedule) expected_min_total_penalty = get_min_total_penalty_bruteforce(deadlines, penalties) actual_total_penalty = get_total_penalty(schedule_ids, deadlines, penalties) assert_that(actual_total_penalty, is_(equal_to(expected_min_total_penalty)))
def get_probabilities_for_optimal_bst(): n = random.randint(1, 10) p, _ = get_random_array(min_size=n, max_size=n) q, _ = get_random_array(min_size=n + 1, max_size=n + 1) q.start = 0 total = sum([x for x in p.elements + q.elements]) for i in between(1, n): p[i] /= total for i in between(0, n): q[i] /= total return p, q
def get_probabilities_for_optimal_bst(): n = random.randint(1, 10) p, _ = get_random_array(min_size=n, max_size=n) q, _ = get_random_array(min_size=n + 1, max_size=n + 1) q.start = 0 total = sum([x for x in p.elements + q.elements]) for i in between(1, n): p[i] /= total for i in between(0, n): q[i] /= total return p, q
def test_binary_add(self): n = random.randint(1, 20) array1, elements1 = get_random_array(min_size=n, max_size=n, max_value=1) array2, elements2 = get_random_array(min_size=n, max_size=n, max_value=1) actual_sum_bits = binary_add(array1, array2) actual_sum = bits_to_number(actual_sum_bits.elements) number1 = bits_to_number(elements1) number2 = bits_to_number(elements2) expected_sum = number1 + number2 assert_that(expected_sum, is_(equal_to(actual_sum)))
def test_effective_fractional_knapsack(self): n = random.randint(1, 10) weights, weights_list = get_random_array(min_size=n, max_size=n) values, values_list = get_random_array(min_size=n, max_size=n) max_weight = random.randint(1, n * 1000) actual_knapsack = effective_fractional_knapsack(weights, values, max_weight) assert_that(sum(actual_knapsack), is_(less_than_or_equal_to(max_weight))) actual_knapsack_value = sum([part_item_value(actual_knapsack[i], weights[i], values[i]) for i in between(1, n)]) knapsack_value_bound = fractional_knapsack_heuristic(Array(weights_list), Array(values_list), max_weight) assert_that(actual_knapsack_value, is_(greater_than_or_equal_to(knapsack_value_bound)))
def test_effective_stack_enqueue(self): capacity = 5 stack1, _ = get_random_array(min_size=capacity, max_size=capacity) stack1.top = random.randint(0, capacity - 1) stack2, _ = get_random_array(min_size=capacity, max_size=capacity) stack2.top = random.randint(0, capacity) x = random.randint(0, 999) expected_elements = get_queue_elements(stack1, stack2) + [x] effective_stack_enqueue(stack1, x) actual_elements = get_queue_elements(stack1, stack2) assert_that(actual_elements, is_(equal_to(expected_elements)))
def test_effective_stack_enqueue(self): capacity = 5 stack1, _ = get_random_array(min_size=capacity, max_size=capacity) stack1.top = random.randint(0, capacity - 1) stack2, _ = get_random_array(min_size=capacity, max_size=capacity) stack2.top = random.randint(0, capacity) x = random.randint(0, 999) expected_elements = get_queue_elements(stack1, stack2) + [x] effective_stack_enqueue(stack1, x) actual_elements = get_queue_elements(stack1, stack2) assert_that(actual_elements, is_(equal_to(expected_elements)))
def test_bitwise_sort(self): array, elements = get_random_array(max_value=1) bitwise_sort(array) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_insertion_sort(self): array, elements = get_random_array() insertion_sort(array) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_heapsort(self): array, elements = get_random_array() heapsort(array) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_knapsack(self): n = random.randint(1, 15) weights, _ = get_random_array(min_size=n, max_size=n) values, _ = get_random_array(min_size=n, max_size=n) max_weight = random.randint(1, n * 1000) captured_output = io.StringIO() actual_knapsack = knapsack(weights, values, max_weight) with redirect_stdout(captured_output): print_knapsack(actual_knapsack, weights, n, max_weight) expected_knapsack_value = knapsack_bruteforce(weights, values, max_weight) assert_that(actual_knapsack[n, max_weight], is_(equal_to(expected_knapsack_value))) actual_items = [int(re.search('a(\d+)', item).group(1)) for item in captured_output.getvalue().splitlines()] actual_knapsack_value_from_items = items_total_value(actual_items, values) assert_that(actual_knapsack_value_from_items, is_(equal_to(expected_knapsack_value)))
def test_randomly_built_tree_quicksort(self): array, elements = get_random_array() randomly_built_tree_quicksort(array, 1, array.length) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_quicksort__(self): array, elements = get_random_array() quicksort__(array, 1, array.length) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_right_stack_pop(self): size = 10 array, _ = get_random_array(min_size=size, max_size=size) array.left_top = random.randint(0, size) array.right_top = random.randint(array.left_top + 1, size + 1) if array.right_top == array.length + 1: assert_that( calling(right_stack_pop).with_args(array), raises(RuntimeError, 'underflow')) else: expected_left_elements = array[1:array.left_top].elements expected_right_elements = array[array.right_top + 1:array.length].elements expected_deleted = array[array.right_top] actual_deleted = right_stack_pop(array) assert_that(actual_deleted, is_(equal_to(expected_deleted))) actual_left_elements = array[1:array.left_top].elements actual_right_elements = array[array.right_top:array. length].elements assert_that(actual_left_elements, is_(equal_to(expected_left_elements))) assert_that(actual_right_elements, is_(equal_to(expected_right_elements)))
def test_binary_add(self): n = random.randint(1, 20) array1, elements1 = get_random_array(min_size=n, max_size=n, max_value=1) array2, elements2 = get_random_array(min_size=n, max_size=n, max_value=1) actual_sum_bits = binary_add(array1, array2) actual_sum = bits_to_number(actual_sum_bits.elements) number1 = bits_to_number(elements1) number2 = bits_to_number(elements2) expected_sum = number1 + number2 assert_that(expected_sum, is_(equal_to(actual_sum)))
def test_count_inversions(self): array, elements = get_random_array() actual_inversions = count_inversions(array, 1, array.length) expected_inversions = sum(len([y for y in elements[i + 1:] if y < x]) for i, x in enumerate(elements)) assert_that(actual_inversions, is_(equal_to(expected_inversions)))
def test_nonincreasing_insertion_sort(self): array, elements = get_random_array() nonincreasing_insertion_sort(array) expected_array = Array(sorted(elements, reverse=True)) assert_that(array, is_(equal_to(expected_array)))
def test_minimum_maximum(self): array, elements = get_random_array() actual_min, actual_max = minimum_maximum(array) assert_that(actual_min, is_(equal_to(min(elements)))) assert_that(actual_max, is_(equal_to(max(elements))))
def test_effective_os_tree(self): _, keys = get_random_array() tree = RedBlackTree(sentinel=OSNode(None)) tree.nil.min = tree.nil.max = tree.nil.pred = tree.nil.succ = tree.nil for key in keys: effective_os_insert(tree, OSNode(key)) nodes = get_binary_tree_nodes(tree, sentinel=tree.nil) while nodes: actual_minimum = effective_os_minimum(tree) actual_maximum = effective_os_maximum(tree) expected_minimum = rb_minimum(tree.root, sentinel=tree.nil) expected_maximum = rb_maximum(tree.root, sentinel=tree.nil) assert_that(actual_minimum, is_(expected_minimum)) assert_that(actual_maximum, is_(expected_maximum)) node = random.choice(nodes) actual_predecessor = effective_os_predecessor(tree, node) actual_successor = effective_os_successor(tree, node) expected_predecessor = rb_predecessor(node, sentinel=tree.nil) expected_successor = rb_successor(node, sentinel=tree.nil) assert_that(actual_predecessor, is_(expected_predecessor)) assert_that(actual_successor, is_(expected_successor)) effective_os_delete(tree, node) nodes = get_binary_tree_nodes(tree, sentinel=tree.nil)
def test_merge_sort(self): array, elements = get_random_array() merge_sort(array, 1, array.length) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_minimum_maximum(self): array, elements = get_random_array() actual_min, actual_max = minimum_maximum(array) assert_that(actual_min, is_(equal_to(min(elements)))) assert_that(actual_max, is_(equal_to(max(elements))))
def test_sets_reordering(self): n = random.randint(1, 8) bases, _ = get_random_array(min_size=n, max_size=n, min_value=1, max_value=10) exponents, _ = get_random_array(min_size=n, max_size=n, min_value=1, max_value=10) sets_reordering(bases, exponents) actual_score = get_score(bases, exponents) expected_score = sets_reordering_bruteforce(bases, exponents) assert_that(actual_score, is_(equal_to(expected_score)))
def test_right_stack_push(self): size = 10 array, _ = get_random_array(min_size=size, max_size=size) array.left_top = random.randint(0, size) array.right_top = random.randint(array.left_top + 1, size + 1) x = random.randint(0, 999) if array.left_top == array.right_top - 1: assert_that( calling(right_stack_push).with_args(array, x), raises(RuntimeError, 'overflow')) else: expected_left_elements = array[1:array.left_top].elements expected_right_elements = [ x ] + array[array.right_top:array.length].elements right_stack_push(array, x) actual_left_elements = array[1:array.left_top].elements actual_right_elements = array[array.right_top:array. length].elements assert_that(actual_left_elements, is_(equal_to(expected_left_elements))) assert_that(actual_right_elements, is_(equal_to(expected_right_elements)))
def test_nonincreasing_insertion_sort(self): array, elements = get_random_array() nonincreasing_insertion_sort(array) expected_array = Array(sorted(elements, reverse=True)) assert_that(array, is_(equal_to(expected_array)))
def test_average_sort(self): array, elements = get_random_array(min_size=2) k = random.randint(1, array.length - 1) average_sort(array, k, 1, array.length) assert_that(array.elements, contains_inanyorder(*elements)) assert_average_sorted(array.elements, k)
def test_build_max_heap_(self): array, elements = get_random_array() build_max_heap_(array) assert_that(array, has_property('heap_size')) assert_that(array.heap_size, is_(equal_to(array.length))) assert_max_heap(array)
def test_tasks_independent(self): n = random.randint(1, 10) deadlines, _ = get_random_array(max_size=n, min_value=1, max_value=n) actual_result = tasks_independent(deadlines, n) expected_result = tasks_independent_bruteforce(deadlines) assert_that(actual_result, is_(equal_to(expected_result)))
def test_insertion_quicksort(self): array, elements = get_random_array(min_size=2) k = random.randint(1, array.length) insertion_quicksort(array, 1, array.length, k) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_insertion_quicksort(self): array, elements = get_random_array(min_size=2) k = random.randint(1, array.length) insertion_quicksort(array, 1, array.length, k) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_young_sort(self): n = random.randint(1, 5) array, elements = get_random_array(min_size=n * n, max_size=n * n) young_sort(array) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_radix_sort(self): d = 5 array, elements = get_random_array(max_value=10**d - 1) radix_sort(array, d) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_tasks_scheduling_(self): n = random.randint(1, 8) deadlines, _ = get_random_array(min_size=n, max_size=n, min_value=1, max_value=n) penalties, _ = get_random_array(min_size=n, max_size=n) actual_schedule = tasks_scheduling_(deadlines, penalties) schedule_ids = decode_tasks(actual_schedule) expected_min_total_penalty = get_min_total_penalty_bruteforce( deadlines, penalties) actual_total_penalty = get_total_penalty(schedule_ids, deadlines, penalties) assert_that(actual_total_penalty, is_(equal_to(expected_min_total_penalty)))
def test_randomized_blackbox_select(self): array, elements = get_random_array() k = random.randint(1, array.length) actual_order_statistic = randomized_blackbox_select(array, 1, array.length, k) expected_order_statistic = sorted(elements)[k - 1] assert_that(actual_order_statistic, is_(equal_to(expected_order_statistic)))
def test_preemptive_act_schedule(self): n = random.randint(1, 4) processing_times, processing_times_elements = get_random_array(min_size=n, max_size=n, min_value=1, max_value=3) release_times, release_times_elements = get_random_array(min_size=n, max_size=n, max_value=15) original_processing_times = Array(processing_times_elements) original_release_times = Array(release_times_elements) sort_activities_by_release_time(processing_times, release_times) release_times.elements.append(math.inf) release_times.length += 1 actual_schedule = preemptive_act_schedule(processing_times, release_times) for i in between(1, n): assert_that(actual_schedule[i], is_(greater_than(release_times[i]))) actual_min_act = sum(actual_schedule.elements) / actual_schedule.length expected_min_act = get_min_preemptive_act_bruteforce(original_processing_times, original_release_times) assert_that(actual_min_act, is_(equal_to(expected_min_act)))
def test_radix_sort(self): d = 5 array, elements = get_random_array(max_value=10 ** d - 1) radix_sort(array, d) expected_array = Array(sorted(elements)) assert_that(array, is_(equal_to(expected_array)))
def test_iterative_randomized_select(self): array, elements = get_random_array() i = random.randint(1, array.length) actual_order_statistic = iterative_randomized_select(array, 1, array.length, i) expected_order_statistic = sorted(elements)[i - 1] assert_that(actual_order_statistic, is_(equal_to(expected_order_statistic)))
def test_act_schedule(self): processing_times, processing_times_elements = get_random_array() original_processing_times = Array(processing_times_elements) actual_schedule = act_schedule(processing_times) actual_min_act = sum(actual_schedule.elements) / actual_schedule.length expected_min_act = get_min_act_bruteforce(original_processing_times) assert_that(actual_min_act, is_(equal_to(expected_min_act)))
def test_counting_in_range(self): k = 20 array, elements = get_random_array(max_value=k) a, b = sorted([random.randint(-10, 30), random.randint(-10, 30)]) actual_count = counting_in_range(array, k, a, b) expected_count = len([x for x in elements if a <= x <= b]) assert_that(actual_count, is_(equal_to(expected_count)))
def test_fractional_knapsack(self): n = random.randint(1, 10) weights, weights_list = get_random_array(min_size=n, max_size=n) values, values_list = get_random_array(min_size=n, max_size=n) max_weight = random.randint(1, n * 1000) actual_knapsack = fractional_knapsack(weights, values, max_weight) assert_that(sum(actual_knapsack), is_(less_than_or_equal_to(max_weight))) actual_knapsack_value = sum([ part_item_value(actual_knapsack[i], weights[i], values[i]) for i in between(1, n) ]) knapsack_value_bound = fractional_knapsack_heuristic( Array(weights_list), Array(values_list), max_weight) assert_that(actual_knapsack_value, is_(greater_than_or_equal_to(knapsack_value_bound)))
def test_unstable_counting_sort(self): k = 20 array, elements = get_random_array(max_value=k) actual_sorted_array = Array.indexed(1, array.length) unstable_counting_sort(array, actual_sorted_array, k) expected_array = Array(sorted(elements)) assert_that(actual_sorted_array, is_(equal_to(expected_array)))
def test_os_count_inversions(self): array, elements = get_random_array() actual_inversions = os_count_inversions(array) expected_inversions = sum( len([y for y in elements[i + 1:] if y < x]) for i, x in enumerate(elements)) assert_that(actual_inversions, is_(equal_to(expected_inversions)))
def test_counting_in_range(self): k = 20 array, elements = get_random_array(max_value=k) a, b = sorted([random.randint(-10, 30), random.randint(-10, 30)]) actual_count = counting_in_range(array, k, a, b) expected_count = len([x for x in elements if a <= x <= b]) assert_that(actual_count, is_(equal_to(expected_count)))
def test_counting_sort(self): k = 20 array, elements = get_random_array(max_value=k) actual_sorted_array = Array.indexed(1, array.length) counting_sort(array, actual_sorted_array, k) expected_array = Array(sorted(elements)) assert_that(actual_sorted_array, is_(equal_to(expected_array)))
def test_median_of_3_partition(self): array, elements = get_random_array() pivot = median_of_3_partition(array, 1, array.length) for x in array[1:pivot]: assert_that(x, is_(less_than_or_equal_to(array[pivot]))) for x in array[pivot + 1:array.length]: assert_that(x, is_(greater_than_or_equal_to(array[pivot])))
def test_inorder_sort(self): array, elements = get_random_array() captured_output = io.StringIO() with redirect_stdout(captured_output): inorder_sort(array) actual_output = [int(x) for x in captured_output.getvalue().splitlines()] assert_that(actual_output, is_(equal_to(sorted(elements))))
def test_act_schedule(self): processing_times, processing_times_elements = get_random_array() original_processing_times = Array(processing_times_elements) actual_schedule = act_schedule(processing_times) actual_min_act = sum(actual_schedule.elements) / actual_schedule.length expected_min_act = get_min_act_bruteforce(original_processing_times) assert_that(actual_min_act, is_(equal_to(expected_min_act)))
def test_iterative_randomized_select(self): array, elements = get_random_array() i = random.randint(1, array.length) actual_order_statistic = iterative_randomized_select( array, 1, array.length, i) expected_order_statistic = sorted(elements)[i - 1] assert_that(actual_order_statistic, is_(equal_to(expected_order_statistic)))
def test_bitonic_tsp(self): n = random.randint(3, 12) xcoords, _ = get_random_array(min_size=n, max_size=n) ycoords, _ = get_random_array(min_size=n, max_size=n) points = Array([Point2D(x, y) for x, y in zip(xcoords, ycoords)]) captured_output = io.StringIO() actual_path_lengths, optimal_paths = bitonic_tsp(points) with redirect_stdout(captured_output): print_bitonic_path(points, optimal_paths) expected_bitonic_path_length = get_shortest_bitonic_path_length_bruteforce(points) assert_that(actual_path_lengths[n, n], is_(close_to(expected_bitonic_path_length, 1e-7))) pattern = re.compile('\((\d+), (\d+)\)') actual_bitonic_path = [Point2D(int(pattern.match(point).group(1)), int(pattern.match(point).group(2))) for point in captured_output.getvalue().splitlines()] assert_that(actual_bitonic_path, has_length(n)) path_length_from_bitonic_path = get_path_length_from_bitonic_path(actual_bitonic_path) assert_that(path_length_from_bitonic_path, is_(close_to(expected_bitonic_path_length, 1e-7)))
def test_effective_stack_dequeue(self): capacity = 5 stack1, _ = get_random_array(min_size=capacity, max_size=capacity) stack1.top = random.randint(0, capacity) stack2, _ = get_random_array(min_size=capacity, max_size=capacity) stack2.top = random.randint(0, capacity) if stack1.top == 0 and stack2.top == 0: assert_that(calling(effective_stack_dequeue).with_args(stack1, stack2), raises(RuntimeError, 'underflow')) else: expected_elements = get_queue_elements(stack1, stack2) del expected_elements[0] expected_deleted = stack2[stack2.top] if stack2.top > 0 else stack1[1] actual_deleted = effective_stack_dequeue(stack1, stack2) assert_that(actual_deleted, is_(equal_to(expected_deleted))) actual_elements = get_queue_elements(stack1, stack2) assert_that(actual_elements, is_(equal_to(expected_elements)))
def test_push(self): size = 10 stack, _ = get_random_array(min_size=size, max_size=size) stack.top = random.randint(0, size - 1) x = random.randint(0, 999) expected_keys = get_stack_elements(stack) + [x] push(stack, x) actual_keys = get_stack_elements(stack) assert_that(actual_keys, is_(equal_to(expected_keys)))
def test_inorder_sort(self): array, elements = get_random_array() captured_output = io.StringIO() with redirect_stdout(captured_output): inorder_sort(array) actual_output = [ int(x) for x in captured_output.getvalue().splitlines() ] assert_that(actual_output, is_(equal_to(sorted(elements))))
def test_greedy_refueling(self): n = random.randint(1, 100) m = random.randint(1, 15) stations, _ = get_random_array(min_size=m + 1, max_size=m + 1, max_value=n) actual_stops = greedy_refueling(stations, n) valid = stops_valid(actual_stops, stations, n) assert_that(valid, is_(True)) expected_min_stops = refueling_bruteforce(stations, n) assert_that(len(actual_stops), is_(equal_to(expected_min_stops)))
def test_linear_search(self): array, elements = get_random_array(min_size=10, max_size=20, max_value=20) v = random.randint(0, 20) actual_index = linear_search(array, v) try: expected_index = elements.index(v) + 1 assert_that(actual_index, is_(equal_to(expected_index))) except ValueError: assert_that(actual_index, is_(none()))
def test_activity_scheduler(self): n = random.randint(1, 15) start_times, _ = get_random_array(min_size=n, max_size=n, max_value=49) finish_times = Array([start_time + random.randint(1, 50) for start_time in start_times]) actual_schedule = activity_scheduler(start_times, finish_times) actual_halls_needed = len(set([hall_number for hall_number in actual_schedule])) expected_halls_needed = max_overlapping_activities(start_times, finish_times) assert_that(actual_halls_needed, is_(equal_to(expected_halls_needed))) assert_schedule_consistent(actual_schedule, start_times, finish_times)