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
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### 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
.clang-format
4 changes: 3 additions & 1 deletion src/bin_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
def binSearch(xs: list[int], x: int):
left, right = 0, len(xs) - 1
while left < right:
if not isinstance(x, int):
raise TypeError(f'X must be int, but {type(x)}')
while left <= right:
Copy link

Choose a reason for hiding this comment

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

Следовало добавить тесты, которые бы покрывали этот случай.

mid = (left + right) // 2
if xs[mid] == x:
return mid
Expand Down
30 changes: 22 additions & 8 deletions src/checksum.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
def modulo11Checksum(ISBNNumber: str):
Copy link

Choose a reason for hiding this comment

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

Название функции и ISBNNumber не соответствует snake_case.


digits = [int(char) for char in ISBNNumber if char.isdigit()]

checkDigit = digits[-1]

if ISBNNumber[-1] == 'x' or ISBNNumber[-1] == 'X':
Copy link

Choose a reason for hiding this comment

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

Если ввести "", то всё упадет с ошибкой IndexError: string index out of range.

digits += [10]
total = 0
for i in range(len(digits) - 1):
weight = 10
if len(digits) != 10:
print("incorrect input, should be 10 numbers")
return False
Comment on lines +6 to +8
Copy link

Choose a reason for hiding this comment

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

Нужно было кидать ошибку, которая бы обрабатывалась во внешнем коде.

for i in range(10):
weight = 10 - i
digit = digits[i]
total += digit * weight
if total % 11 == 0:
print("correct")
return True
else:
print("incorrect")
return False
Comment on lines +13 to +18
Copy link

Choose a reason for hiding this comment

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

Функция modulo11Checksum должна лишь возвращать True/False, добавление вывода в консоль загрязняет её. Вывод в консоль или другие, не относящиеся к сути функции действия, должны происходить в вызывающем коде. Сейчас, если мы захотим использовать ее в if у нас будет мусорный вывод в консоль.


isbnnum = input('input your number: ')
while isbnnum != '-1':
modulo11Checksum(isbnnum)
isbnnum = input('input your number: ')
Comment on lines +20 to +23
Copy link

Choose a reason for hiding this comment

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

Когда pytest импортирует этот файл для тестов, он зависнет, ожидая ввода данных.
Код, который относится к запуску нужно оборачивать в if __name__ == "__main__".

Comment on lines +20 to +23
Copy link

Choose a reason for hiding this comment

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

Нужен блок try-except, который при ошибке уведомлял бы пользователя о сбое в работе, но программа при этом не падала.






checksum = total + checkDigit
return checksum % 11 == 0
6 changes: 5 additions & 1 deletion test/test_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@


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")

def test_with_x():
assert modulo11Checksum("156-881-111-x")