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
2 changes: 2 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
- run: ruff check .

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):
if type(x) != int:
return "wrong type"
Copy link

Choose a reason for hiding this comment

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

Нужно бросать исключение. Такой return может быть неожиданным для пользователя, потому что он ожидает, что возвращаемым значением будет число, а возвращается строка.

left, right = 0, len(xs) - 1
while left < right:
while left <= right:
mid = (left + right) // 2
if xs[mid] == x:
return mid
Expand Down
15 changes: 13 additions & 2 deletions src/checksum.py
Copy link

Choose a reason for hiding this comment

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

В задании требовалось также реализовать консольную утилиту.

Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
def modulo11Checksum(ISBNNumber: str):

digits = [int(char) for char in ISBNNumber if char.isdigit()]
digits = []
for char in ISBNNumber:
if char.isdigit():
digits.append(int(char))
elif (len(digits) == 9) and char == 'X':
digits.append(10)
elif char != '-' and char != ' ':
return False


if len(digits) != 10:
return False
Comment on lines +9 to +14
Copy link

Choose a reason for hiding this comment

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

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


checkDigit = digits[-1]

total = 0
for i in range(len(digits) - 1):
weight = 10
weight = 10 - i
digit = digits[i]
total += digit * weight

Expand Down
6 changes: 6 additions & 0 deletions test/test_bin_searh.py
Copy link

Choose a reason for hiding this comment

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

Не хватает тестов на пустой массив, на массив из 1 элемента.

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ def test_start():

def test_not_in_list():
assert binSearch([1, 2, 3, 4], 5) == -1

def test_wrong_type():
assert binSearch([1,2,3,4,5], 'a') == 'wrong type'

def test_end():
assert binSearch([1,2,3,4,5],5) == 4
11 changes: 6 additions & 5 deletions test/test_checksum.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from src.checksum import modulo11Checksum


def test_good():
assert modulo11Checksum("2-266-11156-8")


def test_bad():
assert not modulo11Checksum("2-266-11156-3")
assert not modulo11Checksum("2-266-11156-8")
assert not modulo11Checksum("1-563-9873X-X")

def test_good():
assert modulo11Checksum("2-266-11156-3")
assert modulo11Checksum("1-563-68745-X")
Copy link

Choose a reason for hiding this comment

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

Image