Draft
Conversation
Godrik0
suggested changes
Dec 3, 2025
Comment on lines
24
to
27
| count = int(input("Input array`s len: ")) | ||
| array = [int(input()) for i in range(count)] | ||
| sort_array = heap_sort(array) | ||
| print(sort_array) |
There was a problem hiding this comment.
Кажется это сломает тесты. При их запуске ты импортируешь файл heap_sort, а так как этот код не находится внутри if __name__ == '__main__': он будет запущен.
| def heap_sort(array): | ||
| lenn = len(array) | ||
| for i in range(lenn // 2 - 1, -1, -1): | ||
| heapify(array, lenn , i) |
| @@ -0,0 +1,27 @@ | |||
| def heapify(array, n, i): | |||
| """ФУнкция для создания кучи""" | |||
There was a problem hiding this comment.
property based тесты с другими реализованными сортировками
Используй для них библиотеку hypothesis
Comment on lines
5
to
32
| def test_empty_list(self): | ||
| self.assertEqual(heap_sort([]), []) | ||
| def test_single_element(self): | ||
| self.assertEqual(heap_sort([7]), [7]) | ||
| def test_sorted_list(self): | ||
| input_list = [1, 2, 3, 4, 5] | ||
| expected = [1, 2, 3, 4, 5] | ||
| self.assertEqual(heap_sort(input_list), expected) | ||
| def test_reverse_sorted_list(self): | ||
| input_list = [5, 4, 3, 2, 1] | ||
| expected = [1, 2, 3, 4, 5] | ||
| self.assertEqual(heap_sort(input_list), expected) | ||
| def test_duplicate_elements(self): | ||
| input_list = [5, 6, 8, 1, 3, 9, 2, 1, 5, 3, 5] | ||
| expected = [1, 1, 2, 3, 3, 5, 5, 5, 6, 8, 9] | ||
| self.assertEqual(heap_sort(input_list), expected) | ||
| def test_negative_numbers(self): | ||
| input_list = [3, -1, 4, -2, 5, 0, -5] | ||
| expected = [-5, -2, -1, 0, 3, 4, 5] | ||
| self.assertEqual(heap_sort(input_list), expected) | ||
| def test_all_same_elements(self): | ||
| input_list = [1, 1, 1] | ||
| expected = [1, 1, 1] | ||
| self.assertEqual(heap_sort(input_list), expected) | ||
| def test_simple_test(self): | ||
| input_list = [23, 45, 12, 90, 1, 13, 24, 17] | ||
| expected = [1, 12, 13, 17, 23, 24, 45, 90] | ||
| self.assertEqual(heap_sort(input_list), expected) |
There was a problem hiding this comment.
Можно переписать, сократив значительное количество кода
def test_heap_sort_cases(self):
test_cases = [
([], [], "empty_list"),
([7], [7], "single_element"),
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], "sorted_list"),
([5, 4, 3, 2, 1], [1, 2, 3, 4, 5], "reverse_sorted"),
([5, 6, 8, 1, 3, 5], [1, 3, 5, 5, 6, 8], "duplicates"),
([3, -1, 0, -5], [-5, -1, 0, 3], "negative_numbers"),
([1, 1, 1], [1, 1, 1], "all_same"),
]
for input_list, expected, case_name in test_cases:
with self.subTest(case=case_name):
self.assertEqual(heap_sort(list(input_list)), expected)Либо перейти на pytest и использовать параметризацию.
added 2 commits
December 13, 2025 16:35
Godrik0
reviewed
Dec 17, 2025
Comment on lines
4
to
25
| class TestProperty: | ||
| def test_sort_result_length(self): | ||
| values = [12, 35, 78, 445, 11, 4, 2, 21, 7] | ||
| lenght = len(values) | ||
| assert len(heap_sort(values)) == lenght | ||
| assert len(bubble_sort(values)) == lenght | ||
| assert len(quick_sort(values)) == lenght | ||
|
|
||
| def test_ordered_output(self): | ||
| values = [6, 3, 23, 67, 78, 21, 81, 13, 5] | ||
| result = heap_sort(values) | ||
| result1 = bubble_sort(values) | ||
| result2 = quick_sort(values) | ||
| for i in range(1, len(result) - 1): | ||
| assert result[i - 1] < result[i] | ||
| assert result1[i - 1] < result1[i] | ||
| assert result2[i - 1] < result2[i] | ||
|
|
||
| def test_heap_sort_other_sort(self): | ||
| values = [9, 8, 7, 6, 5, 4, 3, 2, 1] | ||
| assert heap_sort(values) == quick_sort(values) | ||
| assert heap_sort(values) == bubble_sort(values) No newline at end of file |
There was a problem hiding this comment.
У Вас heap_sort сортирует список на месте, а значит следующие функции получают уже отсортированный массив. Если уж функция выполняет сортировку на месте, то надо передавать ей копию массива heap_sort(values.copy()) .
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.