Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Šoltis <[email protected]>
  • Loading branch information
slimreaper35 committed Jan 17, 2025
1 parent 10f7be7 commit dda82ed
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
pip install --upgrade pip
pip install nox
- name: Test '${{ matrix.nox_env }}' with nox
run: nox -e ${{ matrix.nox_env }}
run: nox -s ${{ matrix.nox_env }}

tests:
name: Unit tests
Expand All @@ -48,7 +48,7 @@ jobs:
pip install nox
pip install coverage
- name: Test with nox
run: nox -e pytest
run: nox -s pytest
- name: Upload coverage report
uses: codecov/[email protected]
with:
Expand Down
3 changes: 1 addition & 2 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ representative at an online or offline event.
## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
<https://www.facebook.com/jumpman2233>.
reported to the community leaders responsible for enforcement.
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand Down
7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ lint.per-file-ignores."__init__.py" = [
"F401", # {name} imported but unused
]
lint.per-file-ignores."tests/*" = [
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"PT011", # pytest.raises({exception}) is too broad, set the match parameter or use a more specific exception
"S101", # Use of assert detected
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"S101", # Use of assert detected
]

[tool.mypy]
Expand Down
4 changes: 2 additions & 2 deletions pysortlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Library containing various sorting algorithms."""

from .sorting import (
from pysortlib.sorting import (
bubble_sort,
heap_sort,
insert_sort,
Expand All @@ -9,4 +9,4 @@
selection_sort,
shell_sort,
)
from .sorting_extra import counting_sort, radix_sort, sleep_sort
from pysortlib.sorting_extra import counting_sort, radix_sort, sleep_sort
26 changes: 13 additions & 13 deletions pysortlib/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def heap_sort(array: list[int]) -> None:
"""
Sorts an array of integers using the heap sort algorithm.
Time complexity: O(n*log(n)), where n is the length of the array.
Extra space complexity: O(1).
Time complexity: O(n log(n)), where n is the length of the array.
Extra space complexity: O(1)
Stable: No (the relative order of equal elements is not preserved).
In-place: Yes (the input array is modified).
Expand Down Expand Up @@ -157,7 +157,7 @@ def merge_sort(array: list[int]) -> list[int]:
"""
Sorts an array of integers using the merge sort algorithm.
Time complexity: O(n*log(n)), where n is the length of the array.
Time complexity: O(n log(n)), where n is the length of the array.
Extra space complexity: O(n), where n is the length of the array.
Stable: Yes (the relative order of equal elements is preserved).
Expand All @@ -181,23 +181,23 @@ def merge_sort(array: list[int]) -> list[int]:


def _partition(array: list[int], start: int, end: int) -> int:
pivot = array[end]
pivot_index = start - 1
partition = array[end]
partition_index = start - 1

for i in range(start, end):
if array[i] <= pivot:
pivot_index += 1
swap(array, pivot_index, i)
if array[i] <= partition:
partition_index += 1
swap(array, partition_index, i)

swap(array, pivot_index + 1, end)
return pivot_index + 1
swap(array, partition_index + 1, end)
return partition_index + 1


def quick_sort(array: list[int], left: int, right: int) -> None:
"""
Sorts an array of integers using the quick sort algorithm.
Time complexity: O(n*log(n)), where n is the length of the array.
Time complexity: O(n log(n)), where n is the length of the array.
Extra space complexity: O(1)
Stable: No (the relative order of equal elements is not preserved).
Expand Down Expand Up @@ -242,7 +242,7 @@ def shell_sort(array: list[int]) -> None:
Sorts an array of integers using the shell sort algorithm.
Time complexity: O(n^2), where n is the length of the array.
Extra space complexity: O(1).
Extra space complexity: O(1)
Stable: No (the relative order of equal elements is not preserved).
In-place: Yes (the input array is modified).
Expand Down Expand Up @@ -272,7 +272,7 @@ def pancake_sort(array: list[int]) -> None:
Sorts an array of integers using the pancake sort algorithm.
Time complexity: O(n^2), where n is the length of the array.
Extra space complexity: O(1).
Extra space complexity: O(1)
Stable: No (the relative order of equal elements is not preserved).
In-place: Yes (the input array is modified).
Expand Down
15 changes: 9 additions & 6 deletions pysortlib/sorting_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from itertools import chain, repeat

from .utils import check_negative_integers
from pysortlib.utils import check_negative_integers


def counting_sort(array: list[int], *, max_value: int) -> list[int]:
Expand Down Expand Up @@ -48,7 +48,7 @@ def radix_sort(array: list[int], *, max_digits: int, base: int = 10) -> list[int
"""
Sorts an array of integers using the radix sort algorithm.
Time complexity: O(d*(n+k)), where:
Time complexity: O(d (n+k)), where:
- d is the number of digits in the largest number
- n is the length of the array
- k is the number of possible digits (the base)
Expand All @@ -62,7 +62,7 @@ def radix_sort(array: list[int], *, max_digits: int, base: int = 10) -> list[int
:param array: array of integers
:param max_digits: number of digits in the largest number
:param base: base of the numbers, default is 10
:param base: base of the numbers
:raises: ValueError: if the array contains negative integers
:return: sorted array of integers
"""
Expand All @@ -79,9 +79,9 @@ def radix_sort(array: list[int], *, max_digits: int, base: int = 10) -> list[int
return array


def _shut_up_for(secs: int) -> int:
time.sleep(secs)
return secs
def _shut_up_for(seconds: int) -> int:
time.sleep(seconds)
return seconds


def sleep_sort(array: list[int]) -> list[int]:
Expand All @@ -92,8 +92,11 @@ def sleep_sort(array: list[int]) -> list[int]:
Extra space complexity: O(n), where n is the length of the array.
:param array: array of integers
:raises: ValueError: if the array contains negative integers
:return: sorted array of integers
"""
check_negative_integers(array)

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(_shut_up_for, num) for num in array]
iterator = concurrent.futures.as_completed(futures)
Expand Down
2 changes: 1 addition & 1 deletion pysortlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ def swap(array: list[int], index_1: int, index_2: int) -> None:
def check_negative_integers(array: list[int]) -> None:
"""Check if the array contains only non-negative integers."""
if any(num < 0 for num in array):
msg = "The array should contain only non-negative integers"
msg = "The array must contain only non-negative integers"
raise ValueError(msg)
21 changes: 17 additions & 4 deletions tests/test_sorting_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_counting_sort(array: list[int]) -> None:
],
)
def test_counting_sort_with_negative_integers(array: list[int]) -> None:
with pytest.raises(ValueError) as exc:
pattern = "The array must contain only non-negative integers"
with pytest.raises(ValueError, match=pattern):
counting_sort(array, max_value=1)
assert str(exc.value) == "The array should contain only non-negative integers"


@given(lists(integers(min_value=0, max_value=999)))
Expand All @@ -42,9 +42,9 @@ def test_radix_sort(array: list[int]) -> None:
],
)
def test_radix_sort_with_negative_integers(array: list[int]) -> None:
with pytest.raises(ValueError) as exc:
pattern = "The array must contain only non-negative integers"
with pytest.raises(ValueError, match=pattern):
radix_sort(array, max_digits=1)
assert str(exc.value) == "The array should contain only non-negative integers"


@pytest.mark.parametrize(
Expand All @@ -65,3 +65,16 @@ def test_sleep_sort(array: list[int]) -> None:

assert (end - start) < timeout
assert result == sorted(array)


@pytest.mark.parametrize(
"array",
[
[-1, 0],
[0, -1],
],
)
def test_sleep_sort_with_negative_integers(array: list[int]) -> None:
pattern = "The array must contain only non-negative integers"
with pytest.raises(ValueError, match=pattern):
radix_sort(array, max_digits=1)

0 comments on commit dda82ed

Please sign in to comment.