from test_helper import run_common_tests, failed, passed, check_tests_pass from partition_souvenirs import partition3 if __name__ == '__main__': run_common_tests() check_tests_pass("partition_souvenirs_unit_tests.py") all_tests_passed = True for values, answer in ( ((20, ), 0), ((7, 7, 7), 1), ((3, 3, 3), 1), ((3, 3, 3, 3), 0), ((1, 2, 3, 4, 5, 5, 7, 7, 8, 10, 12, 19, 25), 1), ([1] * 10, 0), ([1] * 12, 1), ([30] * 20, 0), ([30] * 18, 1), ): if partition3(values) != answer: all_tests_passed = False failed("Wrong answer for values={}".format(values)) break if all_tests_passed: passed()
from random import randint from test_helper import run_common_tests, failed, passed, check_tests_pass from maximum_ad_revenue import max_dot_product def reference(a, b): a = sorted(a) b = sorted(b) return sum(a[i] * b[i] for i in range(len(a))) if __name__ == '__main__': run_common_tests() check_tests_pass("maximum_ad_revenue_unit_tests.py") all_tests_passed = True for n in [10, 20, 30]: a = [randint(0, 10**5) for _ in range(n)] b = [randint(0, 10**5) for _ in range(n)] if reference(a, b) != max_dot_product(a, b): all_tests_passed = False failed("Wrong answer for a={}, b={}".format(a, b)) if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from organizing_lottery import points_cover, points_cover_naive from random import randint if __name__ == '__main__': run_common_tests() check_tests_pass("organizing_lottery_unit_tests.py") all_tests_passed = True for n in (3, 4, 5, 10, 100): for m in (3, 4, 100, 200): points = [randint(-10, 10) for _ in range(m)] starts = [randint(-5, 0) for _ in range(n)] ends = [randint(0, 5) for _ in range(n)] if points_cover(starts, ends, points) != points_cover_naive( starts, ends, points): all_tests_passed = False failed("Wrong answer for starts={}, ends={}, points={}".format( starts, ends, points)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from fibonacci_number import fibonacci_number def fibonacci_number_reference(n): assert 0 <= n <= 40 if n <= 1: return n previous, current = 0, 1 for _ in range(n - 1): previous, current = current, previous + current return current if __name__ == '__main__': run_common_tests() check_tests_pass("fibonacci_number_unit_tests.py") all_tests_passed = True for x in range(41): if fibonacci_number(x) != fibonacci_number_reference(x): all_tests_passed = False failed("Wrong answer for n={}".format(x)) break if all_tests_passed: passed()
left, right = 0, len(keys) while left + 1 < right: ave = (left + right) // 2 if keys[ave] <= query: left = ave else: right = ave if keys[left] != query: return -1 else: return left if __name__ == '__main__': run_common_tests() check_tests_pass("binary_search_unit_tests.py") all_tests_passed = True for _ in range(20): keys = set() for i in range(100): x = randrange(239239) + 1 while x in keys: x = randrange(239239) + 1 keys.add(x) keys = sorted(list(keys)) for key in keys: for query in (key, key + 1): if binary_search(keys, query) != reference(keys, query): all_tests_passed = False
from test_helper import run_common_tests, failed, passed, check_tests_pass from number_of_inversions import compute_inversions, compute_inversions_naive from random import randint if __name__ == '__main__': run_common_tests() check_tests_pass("number_of_inversions_unit_tests.py") all_tests_passed = True for n in (3, 4, 5, 10, 100): for array in ([1] * n, [n - i for i in range(n)], [i for i in range(n)], [randint(0, n) for _ in range(n)]): if compute_inversions(array) != compute_inversions_naive(array): all_tests_passed = False failed("Wrong answer for array={}".format(array)) break if all_tests_passed: passed()
import math from test_helper import run_common_tests, failed, passed, check_tests_pass from maximum_loot import maximum_loot_value if __name__ == '__main__': run_common_tests() check_tests_pass("maximum_loot_unit_tests.py") capacity = 12619 prices = [ 1347, 3494, 4257, 2741, 5711, 1562, 4116, 7471, 5833, 5989, 416, 5515, 2016, 2442, 3905, 3404, 9100, 6207, 1644, 2240, 5231, 1335, 673, 393, 6964, 5535, 6252, 89, 8073, 7775, 2053, 2226, 3563, 693, 9315, 5007, 1829, 9173, 8908, 2625, 5008, 7867, 7532, 7824, 435, 4403, 6028, 5586, 5289, 5541, 2234, 4306, 2299, 8374, 4343, 7359, 8939, 6261, 7100, 8205, 982, 6358, 3990, 570, 2200, 4519, 5251, 5209, 1239, 6045, 8871, 3897, 3756, 143, 7592, 7106, 4762, 1218, 2520, 5230, 1403, 8540, 3841, 6265, 908, 5881, 3717, 8574, 8037, 3239, 4442, 6518, 6564, 4138, 4041, 7052, 2738, 1924, 3326, 6057 ] weights = [ 8254, 9484, 2934, 7521, 5054, 4629, 3648, 2124, 3039, 8661, 9916, 3740, 8876, 3138, 7136, 9804, 2398, 7744, 9785, 5399, 5351, 7907, 8511, 5524, 9136, 5894, 118, 5494, 87, 6297, 4437, 7777, 3549, 2956, 5477, 5542, 7290, 3291, 3019, 7229, 7430, 4829, 7996, 5708, 6681, 7195, 4428, 4293, 7030, 857, 5725, 6936, 7497, 3161, 7268, 9172, 2888, 6389, 7087, 4136, 147, 5791, 1100, 9317, 3256, 3074, 9226, 1652, 4033, 1004, 9477, 2245, 1859, 8303, 3872, 9526, 1678, 5881, 5272, 7923, 6299, 9238, 2039, 7903, 4283, 7011, 6773, 5543, 8704, 1786, 5498, 9117, 7320, 2403, 4692, 4411, 8, 272, 5588, 8540 ]
import math from test_helper import run_common_tests, failed, passed, check_tests_pass from gcd import gcd if __name__ == '__main__': run_common_tests() check_tests_pass("gcd_unit_tests.py") all_tests_passed = True for (a, b) in [(2, 3), (10**8, 10**5 - 1), (10**8, 10**9)]: if gcd(a, b) != math.gcd(a, b): all_tests_passed = False failed("Wrong answer for a={}, b={}".format(a, b)) break if all_tests_passed: passed()
f = [[10**9] * (tn + 2) for _ in range(sn + 2)] f[0][0] = 0 def relax(p, q, x): f[p][q] = min(f[p][q], x) for i in range(sn + 1): for j in range(tn + 1): if i < sn and j < tn: relax(i + 1, j + 1, f[i][j] + (1 if s[i] != t[j] else 0)) relax(i + 1, j, f[i][j] + 1) relax(i, j + 1, f[i][j] + 1) return f[sn][tn] if __name__ == '__main__': run_common_tests() check_tests_pass("edit_distance_unit_tests.py") all_tests_passed = True for first, second in (("abacabadabacabaeabacab", "aeabacabad"), ): if edit_distance(first, second) != reference(first, second): all_tests_passed = False failed("Wrong answer for {} and {}".format(first, second)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from itertools import product from sum_of_two_digits import sum_of_two_digits if __name__ == '__main__': run_common_tests() check_tests_pass("sum_of_two_digits_unit_tests.py") all_tests_passed = True for a, b in product(range(10), repeat=2): if sum_of_two_digits(a, b) != a + b: all_tests_passed = False failed("Wrong answer for a={}, b={}".format(a, b)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from arithmetic_expression import find_maximum_value if __name__ == '__main__': run_common_tests() check_tests_pass("arithmetic_expression_unit_tests.py") all_tests_passed = True for s, answer in ( ("5", 5), ("2+3", 5), ("2-3", -1), ("5-8+7*4-8+9", 200), ("2-3", -1), ("9*9*9*9", 9 * 9 * 9 * 9), ("1-1", 0), ("7", 7), ("1+2+3+4+5+6+7+8+9", 45), ): if find_maximum_value(s) != answer: all_tests_passed = False failed("Wrong answer for {}".format(s)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from maximum_gold import maximum_gold if __name__ == '__main__': run_common_tests() check_tests_pass("maximum_gold_unit_tests.py") all_tests_passed = True for capacity, weights, answer in ( (10, (1, 4, 8), 9), (20, (5, 7, 12, 18), 19), (10, (3, 5, 3, 3, 5), 10), (10, (3, 5, 3, 3, 5), 10), (500, (342, 381, 230, 381, 206, 393, 364, 319, 279, 385, 345, 263, 365, 281, 298, 247, 239, 201, 378, 351), 499) ): if maximum_gold(capacity, weights) != answer: all_tests_passed = False failed("Wrong answer for capacity={}, weights={}".format(capacity, weights)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from primitive_calculator import compute_operations if __name__ == '__main__': run_common_tests() check_tests_pass("primitive_calculator_unit_tests.py") all_tests_passed = True for n, answer in ((20, 4), (200, 8), (239, 10), (69006, 19)): sequence = compute_operations(n) if len(sequence) - 1 != answer: all_tests_passed = False failed("Wrong answer for n={}".format(n)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from maximum_pairwise_product import max_pairwise_product if __name__ == '__main__': run_common_tests() check_tests_pass("maximum_pairwise_product_unit_tests.py") all_tests_passed = True tests = [ ([1, 2], 2), ([2, 1], 2), ([3, 5], 15), ([5, 3], 15), ([7, 7], 49), ([5, 1, 5], 25), ([1, 5, 5], 25), ([i + 1 for i in range(10**5)], (10**5 - 1) * (10**5)), ] for array, answer in tests: if max_pairwise_product(array) != answer: all_tests_passed = False failed("Wrong answer for A={}".format(array)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from closest_points import minimum_distance_squared, minimum_distance_squared_naive, Point from math import fabs from random import randint if __name__ == '__main__': run_common_tests() check_tests_pass("closest_points_unit_tests.py") all_tests_passed = True for points in ([Point(-10**9, -10**9), Point(10**9, 10**9)], [Point(i, i + 1) for i in range(100)], [Point(randint(1, 10), randint(1, 10)) for _ in range(5)], [Point(randint(1, 10), randint(1, 10)) for _ in range(500)]): if fabs( minimum_distance_squared(points) - minimum_distance_squared_naive(points)) > 1e-03: all_tests_passed = False failed("Wrong answer for points={}".format(points)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from money_change import money_change def reference(money): assert 0 <= money <= 10**3 return (money // 10) + ((money % 10) // 5) + (money % 5) if __name__ == '__main__': run_common_tests() check_tests_pass("money_change_unit_tests.py") all_tests_passed = True for m in range(10**3): if money_change(m) != reference(m): all_tests_passed = False failed("Wrong answer for money={}".format(m)) break if all_tests_passed: passed()
if i < len(a): d[i + 1][j][k] = max(d[i][j][k], d[i + 1][j][k]) if j < len(b): d[i][j + 1][k] = max(d[i][j][k], d[i][j + 1][k]) if k < len(c): d[i][j][k + 1] = max(d[i][j][k], d[i][j][k + 1]) if i < len(a) and j < len(b) and k < len( c) and a[i] == b[j] == c[k]: d[i + 1][j + 1][k + 1] = max(d[i + 1][j + 1][k + 1], d[i][j][k] + 1) return d[len(a)][len(b)][len(c)] if __name__ == '__main__': run_common_tests() check_tests_pass("lcs3_unit_tests.py") seed(239) all_tests_passed = True for _ in range(10): for n in (3, 5, 20, 10): for m in (2, 3, 4, 10, 100): lena = randint(1, n) lenb = randint(1, n) lenc = randint(1, n) a = [randint(1, m) for _ in range(lena)] b = [randint(1, m) for _ in range(lenb)] c = [randint(1, m) for _ in range(lenc)]
from test_helper import run_common_tests, failed, passed, check_tests_pass from quicksort import randomized_quick_sort from random import randint if __name__ == '__main__': run_common_tests() check_tests_pass("quicksort_unit_tests.py") all_tests_passed = True for n in (3, 4, 5, 10, 100): for array in ([1] * n, [n - i for i in range(n)], [i for i in range(n)], [randint(0, n) for _ in range(n)]): sorted_array = sorted(list(array)) randomized_quick_sort(array, 0, len(array) - 1) if sorted_array != array: all_tests_passed = False failed("Wrong answer for array={}".format(array)) break if all_tests_passed: passed()
from maximum_salary import largest_number def reference(numbers): numbers = list(map(str, numbers)) for _ in numbers: for i in range(len(numbers) - 1): if numbers[i] + numbers[i + 1] < numbers[i + 1] + numbers[i]: t = numbers[i] numbers[i] = numbers[i + 1] numbers[i + 1] = t return int("".join(numbers)) if __name__ == '__main__': run_common_tests() check_tests_pass("maximum_salary_unit_tests.py") all_tests_passed = True for numbers in [[2, 21, 23, 211, 213, 231, 232], [56, 5, 6, 556, 566, 666, 665, 656]]: if reference(numbers) != largest_number(numbers): all_tests_passed = False failed("Wrong answer for n={}".format(numbers)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from last_digit_of_the_sum_of_squares_of_fibonacci_numbers import last_digit_of_the_sum_of_squares_of_fibonacci_numbers if __name__ == '__main__': run_common_tests() check_tests_pass( "last_digit_of_the_sum_of_fibonacci_numbers_unit_tests.py") all_tests_passed = True for n, d in [(73, 1)]: if last_digit_of_the_sum_of_squares_of_fibonacci_numbers(n) != d: all_tests_passed = False failed("Wrong answer for n={}".format(n)) break if all_tests_passed: passed()
from test_helper import run_common_tests, failed, passed, check_tests_pass from maximum_number_of_prizes import compute_optimal_summands def reference(n): summands = [] k = 1 while n >= k + k + 1: summands.append(k) n -= k k += 1 summands.append(n) return summands if __name__ == '__main__': run_common_tests() check_tests_pass("maximum_number_of_prizes_unit_tests.py") all_tests_passed = True for n in [2, 7, 20, 239, 317]: if reference(n) != compute_optimal_summands(n): all_tests_passed = False failed("Wrong answer for n={}".format(n)) break if all_tests_passed: passed()
for e in elements: if e in table: table[e] += 1 else: table[e] = 1 for e in table: if table[e] > len(elements) / 2: return 1 return 0 if __name__ == '__main__': run_common_tests() check_tests_pass("majority_element_unit_tests.py") all_tests_passed = True for elements, answer in [ ([1, 1, 2], 1), ([1, 2], 0), ([7, 8, 7], 1), ]: if majority_element(elements) != answer: all_tests_passed = False failed("Wrong answer for elements={}".format(elements)) break if all_tests_passed: passed()
def reference(distance, tank, stops): stops = [0] + stops + [distance] num_refills, cur_refill = 0, 0 while cur_refill <= len(stops) - 2: last_refill = cur_refill while cur_refill <= len(stops) - 2 and stops[ cur_refill + 1] - stops[last_refill] <= tank: cur_refill += 1 if cur_refill == last_refill: return -1 if cur_refill <= len(stops) - 2: num_refills += 1 return num_refills if __name__ == '__main__': run_common_tests() check_tests_pass("car_fueling_unit_tests.py") all_tests_passed = True for (d, m, stops) in [(500, 200, [100, 200, 300, 400])]: if reference(d, m, stops) != compute_min_number_of_refills( d, m, stops): all_tests_passed = False failed("Wrong answer for d={}, m={}, stops={}".format(d, m, stops)) break if all_tests_passed: passed()