Skip to content

Comments

work with branches#11

Draft
Gosha924 wants to merge 4 commits intomasterfrom
6_hw_python_heap_sort
Draft

work with branches#11
Gosha924 wants to merge 4 commits intomasterfrom
6_hw_python_heap_sort

Conversation

@Gosha924
Copy link
Owner

No description provided.

@Gosha924 Gosha924 requested a review from chernishev October 15, 2025 17:38
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)
Copy link

Choose a reason for hiding this comment

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

Кажется это сломает тесты. При их запуске ты импортируешь файл 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)
Copy link

Choose a reason for hiding this comment

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

lenn , i не по PEP8

@@ -0,0 +1,27 @@
def heapify(array, n, i):
"""ФУнкция для создания кучи"""
Copy link

Choose a reason for hiding this comment

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

Опечатка

Copy link

Choose a reason for hiding this comment

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

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)
Copy link

Choose a reason for hiding this comment

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

Можно переписать, сократив значительное количество кода

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 и использовать параметризацию.

Copy link

@Godrik0 Godrik0 left a comment

Choose a reason for hiding this comment

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

lenn , i тоже поправить бы

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
Copy link

Choose a reason for hiding this comment

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

У Вас heap_sort сортирует список на месте, а значит следующие функции получают уже отсортированный массив. Если уж функция выполняет сортировку на месте, то надо передавать ей копию массива heap_sort(values.copy()) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants