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
16 changes: 13 additions & 3 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
name: Ruff
on: [push, pull_request]
on: push
jobs:
ruff:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.12.3"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
# Update output format to enable automatic inline annotations.
- name: Run Ruff
run: ruff check --output-format=github .
2 changes: 1 addition & 1 deletion 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):
left, right = 0, len(xs) - 1
while left < right:
while left <= right:
mid = (left + right) // 2
if xs[mid] == x:
return mid
Expand Down
22 changes: 20 additions & 2 deletions src/checksum.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
def modulo11Checksum(ISBNNumber: str):

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


if len(digits) != 10:
print("Цифр должно быть 10", end=' - ')
Copy link

Choose a reason for hiding this comment

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

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

return
Copy link

Choose a reason for hiding this comment

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

Это вернет None, хотя функция предполагает возврат True/False.


Comment on lines +5 to +8
Copy link

Choose a reason for hiding this comment

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

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

checkDigit = digits[-1]

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

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


num = input()

while num != "-1":
res = modulo11Checksum(num)

if res:
print("correct")

else:
print("incorrect")

num = input()
Comment on lines +21 to +32
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: 36 additions & 0 deletions src/test_bin_search.py
Copy link

Choose a reason for hiding this comment

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

А что не так с тестами из папки test? Для чего новые файлы с тестами?

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from bin_search import binSearch
import pytest
import random


def test_nothing():
ls = []
x = random.randint(-1000, 1000)
assert binSearch(ls, x) == -1


def test_one():
ls = [2]
assert binSearch(ls, ls[0]) == 0


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


def test_all():
ls = [x for x in range(0, 100)]
x = random.randint(0, 100)

assert binSearch(ls, ls[x]) == x

def test_all2():
ls = [x for x in range(0, 100)]
x = random.randint(102, 1000)

assert binSearch(ls, x) == -1


if __name__ == '__main__':
pytest.main()
23 changes: 23 additions & 0 deletions src/test_checksum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from checksum import modulo11Checksum
import pytest

def test1():
num = "99921-58-10-7"
assert modulo11Checksum(num) == True
Copy link

Choose a reason for hiding this comment

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

Правильно писать assert modulo11Checksum(num) или assert not modulo11Checksum(num).



def test1_wrong():
num = "99921-58-10-4"
assert modulo11Checksum(num) == False

def test_wiki():
num = "2-266-11156-6"
assert modulo11Checksum(num) == True


def test_wiki_wrong():
num = "2-266-11156-3"
assert modulo11Checksum(num) == False

if __name__ == '__main__':
pytest.main()