-
Notifications
You must be signed in to change notification settings - Fork 22
Теврюков Илья Андреевич Б42 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| 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 | ||
| 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()] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У Вас пропущена обработка символа X, который может служить контрольной цифрой. |
||
| if len(digits) != 10: | ||
| return -1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вы в одном месте возвращаете |
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Когда |
||
| 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 |
| 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-") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def test_bad_wrong_length_long(): | ||
| assert modulo11Checksum("1234567890123") == -1 | ||
|
|
||
|
|
||
| def test_bad_wrong_length_short(): | ||
| assert modulo11Checksum("12345") == -1 | ||
There was a problem hiding this comment.
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)$ .