From 13d48d27cdef11dec8c5f53b6ad381af9f6593ac Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 13:41:35 +0000 Subject: [PATCH 1/7] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82?= =?UTF-8?q?=D0=BC=20=D0=B4=D0=B2=D0=BE=D0=B8=D1=87=D0=BD=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20=D0=BE?= =?UTF-8?q?=D0=B4=D0=BD=D0=BE=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Изменено условие цикла с left < right на left <= right - Исправлена ошибка, из-за которой поиск в одноэлементных массивах выполнялся некорректно. для одноэлементного массива было left = 0, right = 0, 0 < 0 = False, из-за чего цикл не выполнялся ни разу и возвращалось -1 - Теперь корректно возвращает индекс 0 для одноэлементного массива - Поддерживает корректное поведение для пустых и многоэлементных массивов --- src/bin_search.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin_search.py b/src/bin_search.py index 713abfc..3249226 100644 --- a/src/bin_search.py +++ b/src/bin_search.py @@ -1,6 +1,6 @@ -def binSearch(xs: list[int], x: int): +def bin_search(xs: list[int], x: int): left, right = 0, len(xs) - 1 - while left < right: + while left <= right: mid = (left + right) // 2 if xs[mid] == x: return mid From 38d454cc938e5aef71bf2928d572df967dd767da Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 13:54:44 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE:=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=B9=20=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82?= =?UTF-8?q?=20=D0=B2=D0=B5=D1=81=D0=B0=20ISBN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Изменен постоянный вес 10 на уменьшающийся (10-i) - Теперь реализована правильная схема взвешивания ISBN-10: цифра*10 + цифра*9 + ... + цифра*1 --- src/checksum.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/checksum.py b/src/checksum.py index 74b4fb0..e390004 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,14 +1,14 @@ -def modulo11Checksum(ISBNNumber: str): +def modulo11_checksum(isbn_number: str): - digits = [int(char) for char in ISBNNumber if char.isdigit()] + digits = [int(char) for char in isbn_number if char.isdigit()] - checkDigit = digits[-1] + check_digit = digits[-1] total = 0 for i in range(len(digits) - 1): - weight = 10 + weight = 10 - i digit = digits[i] total += digit * weight - checksum = total + checkDigit + checksum = total + check_digit return checksum % 11 == 0 From a92d6741bb971a0b612171b660aabce5b5625cd2 Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 14:00:40 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D1=81?= =?UTF-8?q?=D1=83=D0=BC=D0=BC=D1=8B=20ISBN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Заменено неправильное вычисление контрольной суммы стандартным алгоритмом ISBN-10 - Была проверка (total + check_digit) % 11 == 0 (математически неверно) - Теперь корректно вычисляется: expected_check_digit = (11 - (total % 11)) % 11 - Возвращает значение True, если фактическая контрольная цифра совпадает с ожидаемым значением --- src/checksum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checksum.py b/src/checksum.py index e390004..65e1686 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -10,5 +10,5 @@ def modulo11_checksum(isbn_number: str): digit = digits[i] total += digit * weight - checksum = total + check_digit - return checksum % 11 == 0 + expected_check_digit = (11 - (total % 11)) % 11 + return check_digit == expected_check_digit From 8398f63af153e9c706091c3db45a7bd6ec1ea895 Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 14:05:21 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D0=B8=D0=BD=D1=8B=20ISBN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добавлена проверка того, что ISBN должен содержать ровно 10 цифр (по стандарту) - Возвращает значение False при недопустимых значениях длины (слишком коротких или длинных) --- src/checksum.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/checksum.py b/src/checksum.py index 65e1686..13a6b99 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,6 +1,8 @@ def modulo11_checksum(isbn_number: str): digits = [int(char) for char in isbn_number if char.isdigit()] + if len(digits) != 10: + return False check_digit = digits[-1] From ee5f2c6ffea7fb0242850716e6d5a5d140fa1520 Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 14:19:05 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D1=83=D1=82=D0=B8=D0=BB=D0=B8=D1=82=D1=8B?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20ISBN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Пользователь вводит ISBN, получает correct/incorrect - Завершает работу при вводе -1 - Корректно обрабатывается пустой ввод и различные ошибки --- src/checksum.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/checksum.py b/src/checksum.py index 13a6b99..246b9f0 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -14,3 +14,43 @@ def modulo11_checksum(isbn_number: str): expected_check_digit = (11 - (total % 11)) % 11 return check_digit == expected_check_digit + +def isbn_console_validator(): + """ + Консольная утилита для проверки ISBN-10 номеров. + Пользователь вводит ISBN, получает результат проверки. + Выход по вводу -1. + """ + print("Введите ISBN для проверки или '-1' для выхода") + + while True: + try: + user_input = input("\nВведите ISBN: ").strip() + + # Проверка на выход + if user_input == "-1": + print("Выход из программы. До свидания!") + break + + # Проверка на пустой ввод + if not user_input: + print("Ошибка: Введена пустая строка") + continue + + # Удаляем все пробелы для удобства + isbn_clean = user_input.replace(" ", "") + + # Проверяем ISBN + if modulo11_checksum(isbn_clean): + print("correct") + else: + print("incorrect") + + except KeyboardInterrupt: + print("\n\nПрограмма прервана пользователем. До свидания!") + break + except Exception as e: + print(f"Ошибка: {e}. Пожалуйста, попробуйте еще раз.") + +if __name__ == "__main__": + isbn_console_validator() From e51b0e4680126d589a7e932be42972b2edbadced Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 14:28:46 +0000 Subject: [PATCH 6/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?checksum.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_checksum.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/test/test_checksum.py b/test/test_checksum.py index f64a64c..4d13d97 100644 --- a/test/test_checksum.py +++ b/test/test_checksum.py @@ -1,9 +1,32 @@ -from src.checksum import modulo11Checksum +from src.checksum import modulo11_checksum def test_good(): - assert modulo11Checksum("2-266-11156-8") + assert modulo11_checksum("2-266-11156-8") def test_bad(): - assert not modulo11Checksum("2-266-11156-3") + assert not modulo11_checksum("2-266-11156-3") + +def test_good_simple(): + assert modulo11_checksum("0306406152") + + +def test_bad_simple(): + assert not modulo11_checksum("0306406153") + + +def test_short(): + assert not modulo11_checksum("123") + + +def test_empty(): + assert not modulo11_checksum("") + + +def test_with_spaces(): + assert modulo11_checksum("0 306 40615 2") + + +def test_with_x(): + assert modulo11_checksum("012000030X") From 11cfefddcf416ecb895018275a81685b1997ffb1 Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 7 Nov 2025 14:29:49 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?bin=5Fsearh.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_bin_searh.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/test/test_bin_searh.py b/test/test_bin_searh.py index d18d85b..1193e2c 100644 --- a/test/test_bin_searh.py +++ b/test/test_bin_searh.py @@ -1,13 +1,32 @@ -from src.bin_search import binSearch +from src.bin_search import binary_search def test_middle(): - assert binSearch([1, 2, 3, 4, 5], 4) == 3 + assert binary_search([1, 2, 3, 4, 5], 4) == 3 def test_start(): - assert binSearch([1, 2, 3, 4], 2) == 1 + assert binary_search([1, 2, 3, 4], 2) == 1 def test_not_in_list(): - assert binSearch([1, 2, 3, 4], 5) == -1 + assert binary_search([1, 2, 3, 4], 5) == -1 + +def test_single_element(): + assert binary_search([5], 5) == 0 + + +def test_single_element_not_found(): + assert binary_search([3], 5) == -1 + + +def test_empty_array(): + assert binary_search([], 5) == -1 + + +def test_end(): + assert binary_search([1, 2, 3, 4, 5], 5) == 4 + + +def test_beginning(): + assert binary_search([1, 2, 3, 4, 5], 1) == 0