Skip to content
4 changes: 2 additions & 2 deletions src/bin_search.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
54 changes: 48 additions & 6 deletions src/checksum.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines -3 to 13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У Вас пропущена обработка символа X, который может служить контрольной цифрой.


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()
27 changes: 23 additions & 4 deletions test/test_bin_searh.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
from src.bin_search import binSearch
from src.bin_search import binary_search
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В src.bin_search у Вас функция называется bin_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
29 changes: 26 additions & 3 deletions test/test_checksum.py
Original file line number Diff line number Diff line change
@@ -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")