From ae1a8d47edae9cb2d76494af62cb505abfbd2933 Mon Sep 17 00:00:00 2001 From: Ilya Tevrukov Date: Fri, 7 Nov 2025 13:58:41 +0000 Subject: [PATCH] Fixed binary search algorithm, changed 'left < right' to 'left <= right'. Added comprehensive test suite for binary search. Implemented ISBN validator utility. Fixed weight calculation from 'weight = 10' to 'weight = 10 - i'. Added necessary input validation and error handling. Extended test coverage for all components. --- src/bin_search.py | 23 ++++++++++++----------- src/checksum.py | 39 +++++++++++++++++++++++++-------------- test/test_bin_searh.py | 36 +++++++++++++++++++++++------------- test/test_checksum.py | 30 +++++++++++++++++++++--------- 4 files changed, 81 insertions(+), 47 deletions(-) mode change 100644 => 100755 src/bin_search.py mode change 100644 => 100755 src/checksum.py mode change 100644 => 100755 test/test_bin_searh.py mode change 100644 => 100755 test/test_checksum.py diff --git a/src/bin_search.py b/src/bin_search.py old mode 100644 new mode 100755 index 713abfc..70dc95a --- a/src/bin_search.py +++ b/src/bin_search.py @@ -1,11 +1,12 @@ -def binSearch(xs: list[int], x: int): - left, right = 0, len(xs) - 1 - while left < right: - mid = (left + right) // 2 - if xs[mid] == x: - return mid - if xs[mid] < x: - left = mid + 1 - else: - right = mid - 1 - return -1 +def binSearch(xs: list[int], x: int): + xs = sorted(xs) + left, right = 0, len(xs) - 1 + while left <= right: + mid = (left + right) // 2 + if xs[mid] == x: + return mid + if xs[mid] < x: + left = mid + 1 + else: + right = mid - 1 + return -1 diff --git a/src/checksum.py b/src/checksum.py old mode 100644 new mode 100755 index 74b4fb0..654d9a8 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,14 +1,25 @@ -def modulo11Checksum(ISBNNumber: str): - - digits = [int(char) for char in ISBNNumber if char.isdigit()] - - checkDigit = digits[-1] - - total = 0 - for i in range(len(digits) - 1): - weight = 10 - digit = digits[i] - total += digit * weight - - checksum = total + checkDigit - return checksum % 11 == 0 +def modulo11Checksum(ISBNNumber: str): + digits = [int(char) for char in ISBNNumber if char.isdigit()] + if len(digits) != 10: + return -1 + checkDigit = digits[-1] + total = 0 + for i in range(len(digits) - 1): + weight = 10 - i + digit = digits[i] + total += digit * weight + checksum = total + checkDigit + return checksum % 11 == 0 + +ISBNNumber = input() + +while ISBNNumber != '-1': + res = modulo11Checksum(ISBNNumber) + if res == 1: + print('correct') + elif res == -1: + print('Incorrect number length') + else: + print('incorrect') + ISBNNumber = input() + diff --git a/test/test_bin_searh.py b/test/test_bin_searh.py old mode 100644 new mode 100755 index d18d85b..9a7a50a --- a/test/test_bin_searh.py +++ b/test/test_bin_searh.py @@ -1,13 +1,23 @@ -from src.bin_search import binSearch - - -def test_middle(): - assert binSearch([1, 2, 3, 4, 5], 4) == 3 - - -def test_start(): - assert binSearch([1, 2, 3, 4], 2) == 1 - - -def test_not_in_list(): - assert binSearch([1, 2, 3, 4], 5) == -1 +from src.bin_search import binSearch +def test_middle(): + assert binSearch([1, 2, 3, 4, 5], 4) == 3 + + +def test_start(): + assert binSearch([1, 2, 3, 4], 2) == 1 + + +def test_last_element(): + assert binSearch([1, 2, 3, 4, 5], 5) == 4 + + +def test_single_element_found(): + assert binSearch([42], 42) == 0 + + +def test_single_element_not_found(): + assert binSearch([42], 100) == -1 + + +def test_empty_array(): + assert binSearch([], 5) == -1 diff --git a/test/test_checksum.py b/test/test_checksum.py old mode 100644 new mode 100755 index f64a64c..ffad878 --- a/test/test_checksum.py +++ b/test/test_checksum.py @@ -1,9 +1,21 @@ -from src.checksum import modulo11Checksum - - -def test_good(): - assert modulo11Checksum("2-266-11156-8") - - -def test_bad(): - assert not modulo11Checksum("2-266-11156-3") +from src.checksum import modulo11Checksum + + +def test_bad(): + assert not modulo11Checksum("2-266-11156-3") + + +def test_good_without_hyphens(): + assert modulo11Checksum("2266111568") + + +def test_good_with_x_check_digit(): + assert modulo11Checksum("0-304-33376-") + + +def test_bad_wrong_length_long(): + assert modulo11Checksum("1234567890123") == -1 + + +def test_bad_wrong_length_short(): + assert modulo11Checksum("12345") == -1