Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.3
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
35 changes: 26 additions & 9 deletions src/checksum.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

Рекомендация: при неверной длине строки лучше выбрасывать исключение, чтобы различать некорректный формат входных данных и просто невалидную контрольную сумму.
Тогда в вызывающем коде нужно будет добавить блок try-except.


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")
12 changes: 8 additions & 4 deletions test/test_bin_searh.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 7 additions & 3 deletions test/test_checksum.py
Original file line number Diff line number Diff line change
@@ -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")