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 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" 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 diff --git a/src/17oct25/test_heap_sort.py b/src/17oct25/test_heap_sort.py new file mode 100644 index 0000000..a79f59f --- /dev/null +++ b/src/17oct25/test_heap_sort.py @@ -0,0 +1,55 @@ +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] + +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(): + return random.sample(range(-200, 200), 150) + +def test_property_python_sort(random_int_list): + a = sorted(random_int_list) + b = heap_sort(random_int_list) + assert a == b + diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29