diff --git a/README.md b/README.md index e93444f..798a6fa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +Стариков Егор, группа 142 # Контрольная работа 1 (2 вариант) Темы: Git и GitHub, стандарты оформления кода, хорошие практики, тестирование. diff --git a/src/bin_search.py b/src/bin_search.py index 713abfc..5c60ba9 100644 --- a/src/bin_search.py +++ b/src/bin_search.py @@ -1,10 +1,13 @@ -def binSearch(xs: list[int], x: int): +def bin_search(xs: list[int], x: int): left, right = 0, len(xs) - 1 - while left < right: + for i in range(len(xs) - 1): + if xs[i] > xs[i + 1]: + return -1 + while left <= right: mid = (left + right) // 2 if xs[mid] == x: return mid - if xs[mid] < x: + elif xs[mid] < x: left = mid + 1 else: right = mid - 1 diff --git a/src/checksum.py b/src/checksum.py index 74b4fb0..6628803 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,14 +1,38 @@ -def modulo11Checksum(ISBNNumber: str): +def modulo11checksum(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] + if len(digits) < 9: + return False + + last_char = isbn_number[-1] + if last_char.upper() == 'X': + check_digit = 10 + elif last_char.isdigit(): + check_digit = digits[-1] + else: + return False total = 0 - for i in range(len(digits) - 1): - weight = 10 + for i in range(9): + weight = 10 - i digit = digits[i] total += digit * weight - checksum = total + checkDigit + checksum = total + check_digit return checksum % 11 == 0 + +print("Введите номер:") +s = input() +while True: + if modulo11checksum(s): + print("Все верно") + elif s == "-1": + break + else: + print("Что-то не так") + print("Введите номер:") + s = input() + + + diff --git a/test/test_bin_searh.py b/test/test_bin_searh.py index d18d85b..52b55d5 100644 --- a/test/test_bin_searh.py +++ b/test/test_bin_searh.py @@ -1,13 +1,16 @@ -from src.bin_search import binSearch +from src.bin_search import bin_search def test_middle(): - assert binSearch([1, 2, 3, 4, 5], 4) == 3 + assert bin_search([1, 2, 3, 4, 5], 3) == 2 + +def not_sorted_list(): + assert bin_search([2, 4, 1, 3], 1) == -1 def test_start(): - assert binSearch([1, 2, 3, 4], 2) == 1 + assert bin_search([1, 2, 3, 4], 2) == 1 def test_not_in_list(): - assert binSearch([1, 2, 3, 4], 5) == -1 + assert bin_search([1, 2, 3, 4], 5) == -1 diff --git a/test/test_checksum.py b/test/test_checksum.py index f64a64c..a25285a 100644 --- a/test/test_checksum.py +++ b/test/test_checksum.py @@ -1,9 +1,20 @@ -from src.checksum import modulo11Checksum +from src.checksum import modulo11checksum def test_good(): - assert modulo11Checksum("2-266-11156-8") + assert modulo11checksum("2-266-11156-6") def test_bad(): - assert not modulo11Checksum("2-266-11156-3") + assert not modulo11checksum("2-266-11156-3") + +def test_x(): + assert modulo11checksum("0-8044-2957-X") + +def test_no_correct(): + assert not modulo11checksum("123456789Y") + +def test_len(): + assert not modulo11checksum("31231") + +