diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bebbbab --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +venv + +__pycache__ +.pytest_cache diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..4c8b864 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12.3 \ No newline at end of file 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..6767de2 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,14 +1,31 @@ -def modulo11Checksum(ISBNNumber: str): +def modulo_11_checksum(isbn_number: str): + digits = isbn_number.replace("-", "").replace(" ", "") - digits = [int(char) for char in ISBNNumber if char.isdigit()] - - checkDigit = digits[-1] + if len(digits) != 10: + return False total = 0 - for i in range(len(digits) - 1): - weight = 10 + for i in range(10): digit = digits[i] - total += digit * weight - checksum = total + checkDigit - return checksum % 11 == 0 + if digit.upper() == "X" and i == 9: + val = 10 + elif digit.isdigit(): + val = int(digit) + else: + return False + weight = 10 - i + total += val * weight + + return total % 11 == 0 + + +if __name__ == "__main__": + while True: + isbn_string = input("Put number(-1 exit): ") + if isbn_string == "-1": + break + if modulo_11_checksum(isbn_string): + print("Correct") + else: + print("Incorrect") diff --git a/test/test_bin_searh.py b/test/test_bin_searh.py index d18d85b..e6d3a6d 100644 --- a/test/test_bin_searh.py +++ b/test/test_bin_searh.py @@ -1,13 +1,17 @@ -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], 4) == 3 def test_start(): - assert binSearch([1, 2, 3, 4], 2) == 1 + assert bin_search([1, 2, 3, 4], 2) == 1 + + +def test_one_value(): + assert bin_search([6], 6) == 0 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..2c0c436 100644 --- a/test/test_checksum.py +++ b/test/test_checksum.py @@ -1,9 +1,13 @@ -from src.checksum import modulo11Checksum +from src.checksum import modulo_11_checksum def test_good(): - assert modulo11Checksum("2-266-11156-8") + assert modulo_11_checksum("0-306-40615-2") + + +def test_whith_x(): + assert modulo_11_checksum("0-8044-2957-X") def test_bad(): - assert not modulo11Checksum("2-266-11156-3") + assert not modulo_11_checksum("2-266-11156-3")