def test(self): for first_sequence, second_sequence, answer in ( ((1, 2), (2, 1), 1), ((1, 2), (3, 4), 0), ([17] * 50, [17] * 25, 25), ([1] * 100, [1] * 100, 100), ((2, 7, 5), (2, 5), 2), ((7, ), (1, 2, 3, 4), 0), ((2, 7, 8, 3), (5, 2, 8, 7), 2), ): self.assertEqual(lcs2(first_sequence, second_sequence), answer)
def test(self): for first_sequence, second_sequence, answer in ( ([1, 2], [2, 1], 1), ([1, 2], [3, 4], 0), ([17] * 50, [17] * 25, 25), ([1] * 100, [1] * 100, 100), ([2, 7, 5], [2, 5], 2), ([7], [1, 2, 3, 4], 0), ([2, 7, 8, 3], [5, 2, 8, 7], 2), ([7, 2, 9, 3, 1, 5, 9, 4], [2, 8, 1, 3, 9, 7], 3) ): self.assertEqual(lcs2(first_sequence, second_sequence), answer)
from lcs2 import lcs2 a = '294378418122' b = '9321491847347' print(f"a: {a}") print(f"b: {b}") print("lcs2(a, b):") print(lcs2(a, b))
n, m = len(a), len(b) t = [[0] * (m + 1) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if a[i - 1] == b[j - 1]: t[i][j] = max(t[i - 1][j - 1] + 1, t[i][j - 1], t[i - 1][j]) else: t[i][j] = max(t[i - 1][j], t[i][j - 1]) return t[n][m] if __name__ == '__main__': run_common_tests() check_tests_pass("lcs2_unit_tests.py") all_tests_passed = True for first, second in ( ([1, 2] * 50, [2, 1] * 50), ([0] * 10, [i % 3 for i in range(10)]), ): if lcs2(first, second) != reference(first, second): all_tests_passed = False failed("Wrong answer for {} and {}".format(first, second)) break if all_tests_passed: passed()
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")