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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Стариков Егор, группа 142
# Контрольная работа 1 (2 вариант)

Темы: Git и GitHub, стандарты оформления кода, хорошие практики, тестирование.
Expand Down
9 changes: 6 additions & 3 deletions src/bin_search.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
def binSearch(xs: list[int], x: int):
def bin_search(xs: list[int], x: int):
left, right = 0, len(xs) - 1
while left < right:
for i in range(len(xs) - 1):
if xs[i] > xs[i + 1]:
return -1
Comment on lines +3 to +5
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)$.

while left <= right:
mid = (left + right) // 2
if xs[mid] == x:
return mid
if xs[mid] < x:
elif xs[mid] < x:
left = mid + 1
else:
right = mid - 1
Expand Down
36 changes: 30 additions & 6 deletions src/checksum.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
def modulo11Checksum(ISBNNumber: str):
def modulo11checksum(isbn_number: str):

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

checkDigit = digits[-1]
if len(digits) < 9:
Copy link

Choose a reason for hiding this comment

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

Для ISBN-10 (с учетом X) должно быть ровно 9 или 10 цифр. Сейчас мы можем ввести 1-2-3-4-5 и программа упадет в цикле for i in range(9). Либо вообще ввести "" и всё упадет уже на last_char = isbn_number[-1].

return False

last_char = isbn_number[-1]
if last_char.upper() == 'X':
check_digit = 10
elif last_char.isdigit():
check_digit = digits[-1]
else:
return False

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

checksum = total + checkDigit
checksum = total + check_digit
return checksum % 11 == 0

print("Введите номер:")
s = input()
while True:
if modulo11checksum(s):
print("Все верно")
elif s == "-1":
break
else:
print("Что-то не так")
print("Введите номер:")
s = input()
Comment on lines +25 to +35
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__".




11 changes: 7 additions & 4 deletions test/test_bin_searh.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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], 3) == 2

def not_sorted_list():
Copy link

Choose a reason for hiding this comment

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

pytest по умолчанию ищет функции, начинающиеся с test_. Функция not_sorted_list не будет запущена.

assert bin_search([2, 4, 1, 3], 1) == -1


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


def test_not_in_list():
assert binSearch([1, 2, 3, 4], 5) == -1
assert bin_search([1, 2, 3, 4], 5) == -1
17 changes: 14 additions & 3 deletions test/test_checksum.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from src.checksum import modulo11Checksum
from src.checksum import modulo11checksum


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")
assert not modulo11checksum("2-266-11156-3")

def test_x():
assert modulo11checksum("0-8044-2957-X")

def test_no_correct():
assert not modulo11checksum("123456789Y")

def test_len():
assert not modulo11checksum("31231")