from edit_distance import edit_distance from test.asserts import assert_equal assert_equal(0, edit_distance("ab", "ab"), "sample 1") assert_equal(3, edit_distance("short", "ports"), "sample 2") assert_equal(5, edit_distance("editing", "distance"), "sample 3")
from points_and_segments import naive_count_segments, fast_count_segments from test.asserts import assert_equal segments = [[[0, 5], [7, 10]], [[-10, 10]], [[0, 5], [-3, 2], [7, 10]]] bets = [[1, 6, 11], [-100, 100, 0], [1, 6]] for i in range(0, len(segments)): assert_equal(naive_count_segments(segments[i], bets[i]), fast_count_segments(segments[i], bets[i]), f"sample {i + 1}")
def run_test(a, data): for x in data: assert_equal(linear_search(a, x), binary_search(a, x), str(x))
from knapsack import optimal_weight from test.asserts import assert_equal assert_equal(9, optimal_weight(10, [1, 4, 8]), "sample 1") assert_equal(9, optimal_weight(10, [8, 4, 1]), "sample 1, reversed")
""" Sample 1. Input: 2 Output: 2 2 = 1 + 1. Sample 2. Input: 28 Output: 6 28 = 10 + 10 + 5 + 1 + 1 + 1. """ from change import get_change from test.asserts import assert_equal assert_equal(2, get_change(2), "2") assert_equal(6, get_change(28), "28")
2 5 3 6 Output: 1 3 In this sample, we have three segments: [1,3],[2,5],[3,6] (of length 2,3,3 respectively). All of them contain the point with coordinate 3: 1 ≤ 3 ≤ 3, 2 ≤ 3 ≤ 5, 3 ≤ 3 ≤ 6. Sample 2. Input: 4 4 7 1 3 2 5 5 6 Output: 2 3 6 The second and the third segments contain the point with coordinate 3 while the first and the fourth segments contain the point with coordinate 6. All the four segments cannot be covered by a single point, since the segments [1, 3] and [5, 6] are disjoint. """ assert_equal([3], optimal_points([Segment(1, 3), Segment(2, 5), Segment(3, 6)]), "sample 1") assert_equal([3, 6], optimal_points( [Segment(4, 7), Segment(1, 3), Segment(2, 5), Segment(5, 6)]), "sample 2") assert_equal(output3, optimal_points(sample3), "sample 3")
from placing_parentheses import get_maximum_value from test.asserts import assert_equal assert_equal(6, get_maximum_value("1+5"), "sample 1") assert_equal(200, get_maximum_value("5-8+7*4-8+9"), "sample 2")
from change_dp import get_change from test.asserts import assert_equal """ Sample 1. Input: 2 Output: 2 2 = 1 + 1. Sample 2. Input: 34 Output: 9 34 = 3 + 3 + 4 + 4 + 4 + 4 + 4 + 4 + 4. """ assert_equal(2, get_change(2), "sample 1") assert_equal(9, get_change(34), "sample 2")
def test_solution(): assert_equal(8, get_pisano_period_length(3), "pisano of 3") assert_equal(6, get_pisano_period_length(4), "pisano of 4") assert_equal(20, get_pisano_period_length(5), "pisano of 5") assert_equal(60, get_pisano_period_length(10), "pisano of 10") assert_equal(1500, get_pisano_period_length(1000), "pisano of 1000") assert_equal(1, get_fibonacci_huge(2015, 3), "f(2015) % 3") assert_equal(161, get_fibonacci_huge(239, 1000), "f(239) % 1000") assert_equal(151, get_fibonacci_huge(2816213588, 239), "f(2816213588) % 239")
def test_solution(): assert_equal(0, fibonacci_partial_sum_fast(0, 0), "0...0") assert_equal(1, fibonacci_partial_sum_fast(3, 7), "3...7") assert_equal(5, fibonacci_partial_sum_fast(10, 10), "10...10") assert_equal(2, fibonacci_partial_sum_fast(10, 200), "10...200") assert_equal(5, fibonacci_partial_sum_fast(1, 100000000), "1...100000000")
2 21 2 Output: 221 Note that in this case the above algorithm also returns an incorrect answer 212. Sample 2. Input: 5 9 4 6 1 9 Output: 99641 In this case, the input consists of single-digit numbers only, so the algorithm above computes the right answer. Sample 3. Input: 3 23 39 92 Output: 923923 As a coincidence, for this input the above algorithm produces the right result, though the input does not have any single-digit numbers. """ assert_equal(True, is_greater_than_or_equal("2", "21"), "2 is greater than 21") assert_equal(False, is_greater_than_or_equal("2", "23"), "2 is less than 23") assert_equal(True, is_greater_than_or_equal("3", "23"), "3 is greater than 23") assert_equal("221", largest_number(["21", "2"]), "sample 1") assert_equal("99641", largest_number(["9", "4", "6", "1", "9"]), "sample 2") assert_equal("923923", largest_number(["23", "39", "92"]), "sample 3")
from lcs2 import lcs2 from test.asserts import assert_equal assert_equal(2, lcs2([2, 7, 5], [2, 5]), "sample 1") assert_equal(0, lcs2([2], [1, 2, 3, 4]), "sample 2") assert_equal(2, lcs2([2, 7, 8, 3], [5, 2, 8, 7]), "sample 3")
Input: 1 Output: 0 1 Sample 2. Input: 5 Output: 3 1 2 4 5 Here, we first multiply 1 by 2 two times and then add 1. Another possibility is to first multiply by 3 and then add 1 two times. Hence “1 3 4 5” is also a valid output in this case. Sample 3. Input: 96234 Output: 14 1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234 Again, another valid output in this case is “1 3 9 10 11 33 99 297 891 2673 8019 16038 16039 48117 96234”. """ from primitive_calculator import optimal_sequence from test.asserts import assert_equal assert_equal([1], optimal_sequence(1), "sample 1") assert_equal([1, 2, 4, 5], optimal_sequence(5), "sample 2") assert_equal([ 1, 3, 9, 10, 11, 22, 66, 198, 594, 1782, 5346, 16038, 16039, 32078, 96234 ], optimal_sequence(96234), "sample 3")
from different_summands import optimal_summands from test.asserts import assert_equal """ Sample 1. Input: 6 Output: 3 1 2 3 Sample 2. Input: 8 Output: 3 1 2 5 Sample 3. Input: 2 Output: 1 2 """ assert_equal([1, 2, 3], optimal_summands(6), "sample 1") assert_equal([1, 2, 5], optimal_summands(8), "sample 2") assert_equal([2], optimal_summands(2), "sample 3")
from partition3 import partition3, partition3Naive from test.asserts import assert_equal arrays = [[3, 3, 3, 3], [30], [1, 2, 3, 4, 5, 5, 7, 7, 8, 10, 12, 19, 25]] for i in range(0, len(arrays)): assert_equal(partition3Naive(arrays[i]), partition3(arrays[i]), "sample " + str(i + 1))
from dot_product import max_dot_product from test.asserts import assert_equal """ Sample 1. Input: 1 23 39 Output: 897 897 = 23 · 39. Sample 2. Input: 3 1 3 -5 -2 4 1 Output: 23 23 = 3 · 4 + 1 · 1 + (−5) · (−2). """ assert_equal(897, max_dot_product([23], [39]), "sample 1") assert_equal(23, max_dot_product([1, 3, -5], [-2, 4, 1]), "sample 2")
def test_solution(): for i in range(3, 20): assert_equal(fibonacci_sum_naive(i), fibonacci_sum_fast(i), str(i)) assert_equal(4, fibonacci_sum_fast(3), "3") assert_equal(5, fibonacci_sum_fast(100), "100") assert_equal(3, fibonacci_sum_fast(832564823476), "832564823476")
from sorting import quick_sort3, quick_sort2 from test.asserts import assert_equal i = 2 arrays = [[2, 3, 9, 2, 2], [2, 3, 9, 2, 9], [3] * i + [2] * i + [1] * i] for a in arrays: q2 = a.copy() q3 = a.copy() quick_sort2(q2, 0, len(a) - 1) quick_sort3(q3, 0, len(a) - 1) print(a) print(q2) print(q3) assert_equal(q2, q3, str(a))
Input: 3 50 60 20 100 50 120 30 Output: 180.0000 To achieve the value 180, we take the first item and the third item into the bag. Sample 2. Input: 1 10 500 30 Output: 166.6667 Here, we just take one third of the only available item. """ from fractional_knapsack import get_optimal_value from test.asserts import assert_equal assert_equal( "0.0000", "{:.4f}".format(get_optimal_value(0, [20, 50, 30], [60, 100, 120])), "zero capacity") assert_equal( "180.0000", "{:.4f}".format(get_optimal_value(50, [20, 50, 30], [60, 100, 120])), "sample 1") assert_equal("166.6667", "{:.4f}".format(get_optimal_value(10, [30], [500])), "sample 2")
from majority_element import get_majority_element, get_majority_element_naive from test.asserts import assert_equal arrays = [[2, 2, 2, 1], [2, 3, 9, 2, 2], [1, 2, 3, 4], [1, 2, 3, 1], [ 512766168, 717383758, 5, 126144732, 5, 573799007, 5, 5, 5, 405079772 ]] i = 1 for array in arrays: assert_equal(get_majority_element_naive(array), get_majority_element(array, 0, len(array) - 1), f"sample {i}") i += 1
from inversions import get_number_of_inversions, get_number_of_inversions_naive from test.asserts import assert_equal arrays = [[2, 3, 9, 2, 9], [9, 8, 7, 3, 2, 1]] for a in arrays: assert_equal(get_number_of_inversions_naive(a), get_number_of_inversions(a), "sample 3") """ 9 > 8 9 > 8 9 > 7 9 > 7 9 > 3 9 > 2 9 > 1 8 > 1 8 > 2 8 > 3 8 > 7 8 > 7 7 > 1 7 > 2 7 > 3 7 > 1 7 > 2 7 > 3 3 > 1