Skip to content
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
python -m pip install --upgrade pip
pip install ruff
pip install pytest
pip install -r requirements.txt
# Update output format to enable automatic inline annotations.
- name: Run Ruff
run: ruff check --output-format=github .
Expand Down
Empty file added heapsort/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions heapsort/src/lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")


# Recursive heapsort implementation.
# The default behavior of `cmp` is to check
# whether the first argument is smaller than the second.
# A custom `cmp` is mandatory in case `T` doesn't support comparisons
def heapsort(array: list, cmp: Callable[[T, T], bool] = lambda x, y: x < y) -> list:
n = len(array)

if n < 2:
return array

def heapify(array: list, n: int, i: int) -> list:
left = 2 * i + 1
right = left + 1
largest = i

if left < n and cmp(array[largest], array[left]):
largest = left

if right < n and cmp(array[largest], array[right]):
largest = right

if largest != i:
(array[i], array[largest]) = (array[largest], array[i])
return heapify(array, n, largest)

return array

for i in range(n // 2, -1, -1):
array = heapify(array, n, i)

for i in range(n - 1, 0, -1):
(array[i], array[0]) = (array[0], array[i])
array = heapify(array, i, 0)

return array
Empty file added heapsort/tests/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions heapsort/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from hypothesis import given
from hypothesis import strategies as st

from ..src.lib import heapsort


def test_general():
assert heapsort([0, 8, 3, -1, 5]) == [-1, 0, 3, 5, 8]
assert heapsort([0, 8, 3, -1, 5], cmp=lambda x, y: x > y) == [8, 5, 3, 0, -1]


def test_special():
assert heapsort([]) == []
assert heapsort([1]) == [1]


@given(st.lists(st.integers(), max_size=100))
def test_property(arr):
assert heapsort(arr) == sorted(arr)
Comment on lines +17 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

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

Прикольно



class Custom:
def __init__(self, n: int):
self.inner = n

def __eq__(self, rhs) -> bool:
return self.inner == rhs.inner

def into(self) -> int:
return self.inner


def test_custom():
assert heapsort([Custom(2), Custom(1)], cmp=lambda x, y: x.inner < y.inner) == [
Custom(1),
Custom(2),
]
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
"RET", # Хорошие практики возврата
"SIM", # Общие правила упрощения
]

[tool.pytest.ini_options]
python_files = "*.py"
testpaths = ["./*/tests"]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hypothesis