From c6b0af07ae1a9fdddb4c354075123acef7378d52 Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 7 Nov 2025 15:53:56 +0300 Subject: [PATCH 1/6] =?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.gitignore=20=D0=B8=20.python-version=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B1=D0=B8=D0=BD=D0=B0=D1=80=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=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87=D0=B5=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=B8=D0=B7=20=D0=BE=D0=B4?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=8D=D0=BB=D0=BB=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 23 +++++++++++++++++++++++ .python-version | 1 + src/bin_search.py | 4 ++-- src/checksum.py | 9 ++++----- test/test_bin_searh.py | 12 ++++++++---- test/test_checksum.py | 6 +++--- 6 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 .gitignore create mode 100644 .python-version 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..c557ab1 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,8 +1,7 @@ -def modulo11Checksum(ISBNNumber: str): +def modulo_11_checksum(isbn_number: str): + digits = [int(char) for char in isbn_number if char.isdigit()] - digits = [int(char) for char in ISBNNumber if char.isdigit()] - - checkDigit = digits[-1] + check_digit = digits[-1] total = 0 for i in range(len(digits) - 1): @@ -10,5 +9,5 @@ def modulo11Checksum(ISBNNumber: str): digit = digits[i] total += digit * weight - checksum = total + checkDigit + checksum = total + check_digit return checksum % 11 == 0 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..a820080 100644 --- a/test/test_checksum.py +++ b/test/test_checksum.py @@ -1,9 +1,9 @@ -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("2-266-11156-8") def test_bad(): - assert not modulo11Checksum("2-266-11156-3") + assert not modulo_11_checksum("2-266-11156-3") From ac6e53fb12258c4d3e6e16fe191628d97512454f Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 7 Nov 2025 16:12:57 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B0=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/checksum.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/checksum.py b/src/checksum.py index c557ab1..d4ff364 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -1,5 +1,8 @@ def modulo_11_checksum(isbn_number: str): - digits = [int(char) for char in isbn_number if char.isdigit()] + digits = isbn_number.replace("-", "").replace(" ", "") + + if len(digits) != 10: + return False check_digit = digits[-1] From e190a487e7ef271ee7a5b0edc05db962fd3f66af Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 7 Nov 2025 16:19:00 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=A3=D0=BC=D0=B5=D0=BD=D1=8C=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20weight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/checksum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checksum.py b/src/checksum.py index d4ff364..69399d0 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -7,9 +7,9 @@ def modulo_11_checksum(isbn_number: str): check_digit = digits[-1] total = 0 - for i in range(len(digits) - 1): - weight = 10 + for i in range(10): digit = digits[i] + weight = 10 - i total += digit * weight checksum = total + check_digit From 141ffaeffc0856eb64667c1ae8cea6a4e1d76897 Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 7 Nov 2025 16:41:31 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=94=D0=9E=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20X?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/checksum.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/checksum.py b/src/checksum.py index 69399d0..0621da2 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -4,13 +4,20 @@ def modulo_11_checksum(isbn_number: str): if len(digits) != 10: return False - check_digit = digits[-1] - total = 0 for i in range(10): digit = digits[i] + + if digit.upper() == "X" and i == 9: + val = 10 + elif digit.isdigit(): + val = int(digit) + else: + return False weight = 10 - i - total += digit * weight + total += val * weight + + return total % 11 == 0 + - checksum = total + check_digit - return checksum % 11 == 0 +print(modulo_11_checksum("123456789X")) From 8c3ccd0511160fd01dbd2e82b948ab040a56761b Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 7 Nov 2025 16:55:34 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20isbn,=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=B8=D0=BC?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/checksum.py | 10 +++++++++- test/test_checksum.py | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/checksum.py b/src/checksum.py index 0621da2..5570fcc 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -20,4 +20,12 @@ def modulo_11_checksum(isbn_number: str): return total % 11 == 0 -print(modulo_11_checksum("123456789X")) +if __name__ == "__main__": + while True: + isbn_string = input("Введите номер(-1 выход): ") + if isbn_string == "-1": + break + if modulo_11_checksum(isbn_string): + print("Корректен") + else: + print("Не корректен") diff --git a/test/test_checksum.py b/test/test_checksum.py index a820080..2c0c436 100644 --- a/test/test_checksum.py +++ b/test/test_checksum.py @@ -2,7 +2,11 @@ def test_good(): - assert modulo_11_checksum("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(): From c112c7952052ad828437ceb4ae05e4382683e3f7 Mon Sep 17 00:00:00 2001 From: Karim Date: Fri, 7 Nov 2025 17:09:48 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BD=D0=B0=20=D1=8F?= =?UTF-8?q?=D0=B7=D1=8B=D0=BA=D0=B0=20=D0=B2=20=D1=83=D1=82=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/checksum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/checksum.py b/src/checksum.py index 5570fcc..6767de2 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -22,10 +22,10 @@ def modulo_11_checksum(isbn_number: str): if __name__ == "__main__": while True: - isbn_string = input("Введите номер(-1 выход): ") + isbn_string = input("Put number(-1 exit): ") if isbn_string == "-1": break if modulo_11_checksum(isbn_string): - print("Корректен") + print("Correct") else: - print("Не корректен") + print("Incorrect")