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

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/bin_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
def binSearch(xs: list[int], x: int):
def bin_search(xs: list[int], x: int):

if type(x) != int:
raise "x is not int"
Copy link

Choose a reason for hiding this comment

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

В Python 3.x мы уже не можем кидать строки как исключения.


left, right = 0, len(xs) - 1
while left < right:
while left <= right:
Copy link

Choose a reason for hiding this comment

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

Нет теста на этот случай.

mid = (left + right) // 2
if xs[mid] == x:
return mid
Expand All @@ -9,3 +13,4 @@ def binSearch(xs: list[int], x: int):
else:
right = mid - 1
return -1

28 changes: 26 additions & 2 deletions src/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,37 @@ def modulo11Checksum(ISBNNumber: str):

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

checkDigit = digits[-1]
assert (len(digits) == 9 and ISBNNumber[-1] == "X") or len(digits) == 10, "invalid lenght"
Copy link

Choose a reason for hiding this comment

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

Валидация на assert это плохо, он предназначен для отладки, и при запуске с флагом -O все ассерты вырезаются.


if (ISBNNumber[-1] == "X"):
#trash number
digits += [-1]
checkDigit = 10
else:
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

if __name__ == "__main__":
while True:
isbn = input("Enter number: ")

if isbn == "-1":
break

try:
result = modulo11Checksum(isbn)
if result:
print("correct")
else:
print("incorrect")
except Exception as e:
print(f"{type(e).__name__}: {e}")

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


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


def test_binary_lenght_middle_left():
assert bin_search([1, 2, 3, 4, 5, 6, 7, 8], 4) == 3


def test_binary_lenght_middle_right():
assert bin_search([1, 2, 3, 4, 5, 6, 7, 8], 5) == 4

2 changes: 1 addition & 1 deletion test/test_checksum.py
Copy link

Choose a reason for hiding this comment

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

Хотелось бы еще тесты с X

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


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


def test_bad():
Expand Down