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
Empty file added src/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions src/heapsort.py
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):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Слишком много у вас break

while True:
lef, rig = i * 2 + 1, i * 2 + 2
Copy link
Collaborator

Choose a reason for hiding this comment

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

Почему не назвать честно left и right?


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 added tests/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions tests/conftest.py
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)]
61 changes: 61 additions & 0 deletions tests/test_heapsort.py
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

У вас точно эти тесты работают? Вы же не импортировали conftest