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: 12 additions & 11 deletions src/bin_search.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
def binSearch(xs: list[int], x: int):
left, right = 0, len(xs) - 1
while left < right:
mid = (left + right) // 2
if xs[mid] == x:
return mid
if xs[mid] < x:
left = mid + 1
else:
right = mid - 1
return -1
def binSearch(xs: list[int], x: int):
xs = sorted(xs)
Copy link

Choose a reason for hiding this comment

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

Бинарный поиск должен работать за $O(\log{N})$, добавляя сортировку он начинает работать за $O(N)$.

left, right = 0, len(xs) - 1
while left <= right:
mid = (left + right) // 2
if xs[mid] == x:
return mid
if xs[mid] < x:
left = mid + 1
else:
right = mid - 1
return -1
39 changes: 25 additions & 14 deletions src/checksum.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
def modulo11Checksum(ISBNNumber: str):

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

checkDigit = digits[-1]

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

checksum = total + checkDigit
return checksum % 11 == 0
def modulo11Checksum(ISBNNumber: str):
digits = [int(char) for char in ISBNNumber if char.isdigit()]
Copy link

Choose a reason for hiding this comment

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

У Вас пропущена обработка символа X, который может служить контрольной цифрой.

if len(digits) != 10:
return -1
Copy link

Choose a reason for hiding this comment

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

Вы в одном месте возвращаете -1, а в другом bool. В Python -1 оценивается как True в булевом контексте.

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

ISBNNumber = input()

while ISBNNumber != '-1':
res = modulo11Checksum(ISBNNumber)
if res == 1:
print('correct')
elif res == -1:
print('Incorrect number length')
else:
print('incorrect')
ISBNNumber = input()

Comment on lines +14 to +25
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__".

36 changes: 23 additions & 13 deletions test/test_bin_searh.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from src.bin_search import binSearch


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


def test_start():
assert binSearch([1, 2, 3, 4], 2) == 1


def test_not_in_list():
assert binSearch([1, 2, 3, 4], 5) == -1
from src.bin_search import binSearch
def test_middle():
assert binSearch([1, 2, 3, 4, 5], 4) == 3


def test_start():
assert binSearch([1, 2, 3, 4], 2) == 1


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


def test_single_element_found():
assert binSearch([42], 42) == 0


def test_single_element_not_found():
assert binSearch([42], 100) == -1


def test_empty_array():
assert binSearch([], 5) == -1
30 changes: 21 additions & 9 deletions test/test_checksum.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from src.checksum import modulo11Checksum


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


def test_bad():
assert not modulo11Checksum("2-266-11156-3")
from src.checksum import modulo11Checksum


def test_bad():
assert not modulo11Checksum("2-266-11156-3")


def test_good_without_hyphens():
assert modulo11Checksum("2266111568")


def test_good_with_x_check_digit():
assert modulo11Checksum("0-304-33376-")
Copy link

Choose a reason for hiding this comment

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

modulo11Checksum выкинет дефисы, получит 9 цифр, вернет -1, а assert сработает как True и тест пройдет.



def test_bad_wrong_length_long():
assert modulo11Checksum("1234567890123") == -1


def test_bad_wrong_length_short():
assert modulo11Checksum("12345") == -1