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 diff --git a/src/checksum.py b/src/checksum.py index 74b4fb0..246b9f0 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,14 +1,56 @@ -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()] + if len(digits) != 10: + return False - 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 - return checksum % 11 == 0 + 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() 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 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")