-
Notifications
You must be signed in to change notification settings - Fork 0
add heapsort #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
Open
shannami
wants to merge
2
commits into
main
Choose a base branch
from
homework_pytets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add heapsort #4
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| def swap(arr, i, j): | ||
| arr[i], arr[j] = arr[j], arr[i] | ||
|
|
||
|
|
||
| def sift_down(arr, i, upper): | ||
| while True: | ||
| lef, rig = i * 2 + 1, i * 2 + 2 | ||
|
Collaborator
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. Почему не назвать честно |
||
|
|
||
| if rig < upper: | ||
| if arr[i] >= max(arr[lef], arr[rig]): | ||
| return | ||
| if arr[lef] > arr[rig]: | ||
| swap(arr, i, lef) | ||
| i = lef | ||
| else: | ||
| swap(arr, i, rig) | ||
| i = rig | ||
|
|
||
| elif lef < upper: | ||
| if arr[lef] > arr[i]: | ||
| swap(arr, i, lef) | ||
| i = lef | ||
| else: | ||
| return | ||
|
|
||
| else: | ||
| return | ||
|
|
||
|
|
||
| def heapsort(arr): | ||
| for j in range((len(arr) - 2) // 2, -1, -1): | ||
| sift_down(arr, j, len(arr)) | ||
|
|
||
| for end in range(len(arr) - 1, 0, -1): | ||
| swap(arr, 0, end) | ||
| sift_down(arr, 0, end) | ||
|
|
||
|
|
||
| arr = [4, 90, 6, 14, 35, 7] | ||
| heapsort(arr) | ||
| print(arr) | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import pytest | ||
| import random | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_list(): | ||
| return [4, 90, 6, 14, 35, 7] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def many_duplicates(): | ||
| return [i % 6 for i in range(200)] | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| params=[ | ||
| [], | ||
| [1], | ||
| [2, 1], | ||
| [5, 5, 5, 5], | ||
| [10, -1, 3, 5], | ||
| ] | ||
| ) | ||
| def case_list(request): | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def random_list(): | ||
| random.seed(52) | ||
| return [random.randint(-1000, 1000) for i in range(100)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import pytest | ||
| from src.heapsort import heapsort | ||
|
|
||
|
|
||
| def test_heapsort(): | ||
| a = [3, 1, 2] | ||
| heapsort(a) | ||
| assert a == [1, 2, 3] | ||
|
|
||
|
|
||
| def test_negative_heapsort(): | ||
| a = [-3, -1, -2] | ||
| heapsort(a) | ||
| assert a == [-3, -2, -1] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_list, expected", | ||
| [ | ||
| ([], []), | ||
| ([1], [1]), | ||
| ([2, 2, 2], [2, 2, 2]), | ||
| ([4, 3, 2, 1], [1, 2, 3, 4]), | ||
| ], | ||
| ) | ||
| def test_parametrized(input_list, expected): | ||
| arr = list(input_list) | ||
| heapsort(arr) | ||
| assert arr == expected | ||
|
|
||
|
|
||
| def test_duplicates_and_negatives(): | ||
| arr = [0, -1, 5, -1, 3, 0, 5] | ||
| expected = sorted(arr) | ||
| heapsort(arr) | ||
| assert arr == expected | ||
|
|
||
|
|
||
| def test_heapsort_sample_list(sample_list): | ||
| arr = sample_list.copy() | ||
| heapsort(arr) | ||
| assert arr == sorted(sample_list) | ||
|
|
||
|
|
||
| def test_heapsort_many_duplicates(many_duplicates): | ||
| arr = many_duplicates.copy() | ||
| heapsort(arr) | ||
| assert arr == sorted(many_duplicates) | ||
|
|
||
|
|
||
| def test_heapsort_cases(case_list): | ||
| arr = list(case_list) | ||
| heapsort(arr) | ||
| assert arr == sorted(case_list) | ||
|
|
||
|
|
||
| def test_heapsort_random(random_list): | ||
| a = random_list.copy() | ||
| b = list(a) | ||
| heapsort(b) | ||
| assert b == sorted(a) | ||
|
Comment on lines
+39
to
+61
Collaborator
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. У вас точно эти тесты работают? Вы же не импортировали |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Слишком много у вас
break