From 20e5dd0cf6a9a245f1e3f7b9b65fe8f31e2f5b61 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Tue, 21 Oct 2025 14:57:37 +0300 Subject: [PATCH 1/6] Create heap sort function --- src/17oct25/heap_sort.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/17oct25/heap_sort.py diff --git a/src/17oct25/heap_sort.py b/src/17oct25/heap_sort.py new file mode 100644 index 0000000..4de48ae --- /dev/null +++ b/src/17oct25/heap_sort.py @@ -0,0 +1,18 @@ +def heap(collection, index, length): + head = index + if index * 2 + 1 < length and collection[head] < collection[index * 2 + 1]: + head = index * 2 + 1 + if index * 2 + 2 < length and collection[head] < collection[index * 2 + 2]: + head = index * 2 + 2 + if head != index: + collection[index], collection[head] = collection[head], collection[index] + heap(collection, head, length) + +def heap_sort(collection): + n = len(collection) + for i in range(n // 2, -1, -1): + heap(collection, i, n) + for i in range(n - 1, 0, -1): + collection[i], collection[0] = collection[0], collection[i] + heap(collection, 0, i) + return collection From d388dc7b8b8250da99ff7acb632a27e1453dae66 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Tue, 21 Oct 2025 15:01:20 +0300 Subject: [PATCH 2/6] Add pytest package --- pyproject.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f602780..a199642 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,9 @@ version = "0.1.0" description = "Homeworks" readme = "README.md" requires-python = ">=3.13.7" -dependencies = [] +dependencies = [ + "pytest>=8.4.2", +] [tool.ruff] exclude = [ @@ -59,4 +61,4 @@ extend-select = [ quote-style = "double" indent-style = "space" skip-magic-trailing-comma = false -line-ending = "auto" \ No newline at end of file +line-ending = "auto" From ba3a45beacf45b55980acf532b1ecc030224e300 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Tue, 21 Oct 2025 19:01:40 +0300 Subject: [PATCH 3/6] Tests for heap sort --- src/17oct25/test_heap_sort.py | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/17oct25/test_heap_sort.py diff --git a/src/17oct25/test_heap_sort.py b/src/17oct25/test_heap_sort.py new file mode 100644 index 0000000..43e1c32 --- /dev/null +++ b/src/17oct25/test_heap_sort.py @@ -0,0 +1,54 @@ +from heap_sort import heap_sort +import pytest +import random + +def test_simple(): + assert heap_sort([3, 2, 1, 5, 10, 8]) == [1, 2, 3, 5, 8, 10] + +def test_short_list(): + assert heap_sort([42, 0]) == [0, 42] + +def test_long_list(): + a = [93, 49, 39, 24, 69, 72, 90, 38, 55, 24, 63, 23, 39, 84, 35, 68, 80, 17, 12, 94, 69, 37, 97, 89, 16, 53, 96, 13] + b = [12, 13, 16, 17, 23, 24, 24, 35, 37, 38, 39, 39, 49, 53, 55, 63, 68, 69, 69, 72, 80, 84, 89, 90, 93, 94, 96, 97] + heap_sort(a) + assert a == b + +def test_list_of_strings(): + assert heap_sort(["p", "y", "t", "h", "o", "n"]) == ['h', 'n', 'o', 'p', 't', 'y'] + +def test_self(): + assert heap_sort([40, 8, 25, 9, 37, 23, 41, 18, 35, 24]) == heap_sort([40, 8, 25, 9, 37, 23, 41, 18, 35, 24]) + +@pytest.mark.parametrize( + ["input_data", "expected_out"], + [ + ([1, 3, 2], [1, 2, 3]), + ([10**10, 10**9, 10**5, 10**8, 10**2, 10**7], [10**2, 10**5, 10**7, 10**8, 10**9, 10**10]), + ([651, -86, -152, 384, 245, 428, -29, -839, -575, -472], [-839, -575, -472, -152, -86, -29, 245, 384, 428, 651]), + ([313, 665, -641, -435, -819, -420, 801, 208], [-819, -641, -435, -420, 208, 313, 665, 801]), + ([-5505, 9828, 9045, 969, 1508, -8066, -8690], [-8690, -8066, -5505, 969, 1508, 9045, 9828]), + ([-35.120, 28.864, 54.075, -5.354, 57.778, 43.898], [-35.120, -5.354, 28.864, 43.898, 54.075, 57.778]), + ([1.3, 1.2, 1.1, 1.0], [1.0, 1.1, 1.2, 1.3]), + ] +) +def test_another(input_data, expected_out): + assert heap_sort(input_data) == expected_out + +@pytest.mark.xfail(raises=TypeError) +def test_incorrect_type(): + assert heap_sort(1) + +def test_empty_list(): + assert heap_sort([]) == [] + +@pytest.fixture() +def random_int_list(): + random_list = random.sample(range(-200, 200), 150) + return random_list + +def test_property_python_sort(random_int_list): + a = sorted(random_int_list) + b = heap_sort(random_int_list) + assert a == b + From 339084d6d34dcb7a1828bc2fe28bbcc8c5bf647e Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Wed, 22 Oct 2025 18:04:05 +0300 Subject: [PATCH 4/6] Make src package folder --- src/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/__init__.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 From a372bad19458e7ec6b7ccac1196e480e865ca0a5 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Wed, 22 Oct 2025 18:07:42 +0300 Subject: [PATCH 5/6] Tests in CI --- .github/workflows/main.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 12e8443..b5441f5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,5 +16,8 @@ jobs: run: | python -m pip install --upgrade pip uv tool install ruff + uv tool install pytest - name: Run Ruff - run: uvx ruff check --output-format=github . \ No newline at end of file + run: uvx ruff check --output-format=github . + - name: Run tests + run: uvx pytest --maxfail=1 --disable-warnings -q \ No newline at end of file From 19c50ae27cf515c71eb1820ccc71ccb7da88e1f1 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Wed, 22 Oct 2025 18:16:55 +0300 Subject: [PATCH 6/6] remove Unnecessary assignment --- src/17oct25/test_heap_sort.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/17oct25/test_heap_sort.py b/src/17oct25/test_heap_sort.py index 43e1c32..a79f59f 100644 --- a/src/17oct25/test_heap_sort.py +++ b/src/17oct25/test_heap_sort.py @@ -1,7 +1,9 @@ -from heap_sort import heap_sort -import pytest import random +import pytest +from heap_sort import heap_sort + + def test_simple(): assert heap_sort([3, 2, 1, 5, 10, 8]) == [1, 2, 3, 5, 8, 10] @@ -44,8 +46,7 @@ def test_empty_list(): @pytest.fixture() def random_int_list(): - random_list = random.sample(range(-200, 200), 150) - return random_list + return random.sample(range(-200, 200), 150) def test_property_python_sort(random_int_list): a = sorted(random_int_list)