diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 164630d..c12fb02 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -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 . diff --git a/src/bin_search.py b/src/bin_search.py index 713abfc..3c91f7d 100644 --- a/src/bin_search.py +++ b/src/bin_search.py @@ -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 diff --git a/src/checksum.py b/src/checksum.py index 74b4fb0..838002b 100644 --- a/src/checksum.py +++ b/src/checksum.py @@ -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=' - ') + return + 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() diff --git a/src/test_bin_search.py b/src/test_bin_search.py new file mode 100644 index 0000000..e7b22b5 --- /dev/null +++ b/src/test_bin_search.py @@ -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() diff --git a/src/test_checksum.py b/src/test_checksum.py new file mode 100644 index 0000000..7b9d5fa --- /dev/null +++ b/src/test_checksum.py @@ -0,0 +1,23 @@ +from checksum import modulo11Checksum +import pytest + +def test1(): + num = "99921-58-10-7" + assert modulo11Checksum(num) == True + + +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()