From 283c2d7a32576593300ef8d9faa89cb3a32cb23e Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Fri, 6 Oct 2023 22:16:58 -0300 Subject: [PATCH 1/5] add some documentation --- sorts/heap_sort.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 4dca879bd89c..54cfd80913d8 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -9,26 +9,35 @@ For manual testing run: python heap_sort.py """ +from typing import List -def heapify(unsorted, index, heap_size): +def heapify(unsorted_list: List[int], index: int, heap_size: int) -> None: + """ + + :param unsorted_list: unsorted list containing integers numbers + :param index: index + :param heap_size: size of the heap + :return: None + """ largest = index left_index = 2 * index + 1 right_index = 2 * index + 2 - if left_index < heap_size and unsorted[left_index] > unsorted[largest]: + if left_index < heap_size and unsorted_list[left_index] > unsorted_list[largest]: largest = left_index - if right_index < heap_size and unsorted[right_index] > unsorted[largest]: + if right_index < heap_size and unsorted_list[right_index] > unsorted_list[largest]: largest = right_index if largest != index: - unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] - heapify(unsorted, largest, heap_size) + unsorted_list[largest], unsorted_list[index] = unsorted_list[index], unsorted_list[largest] + heapify(unsorted_list, largest, heap_size) -def heap_sort(unsorted): +def heap_sort(unsorted_list: List[int]) -> List[int]: """ Pure implementation of the heap sort algorithm in Python + :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending @@ -42,17 +51,20 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] + + >>> heap_sort([3, 7, 9, 28, 123, -5, 8, -30, -200, 0, 4]) + [-200, -30, -5, 0, 3, 4, 7, 8, 9, 28, 123] """ - n = len(unsorted) + n = len(unsorted_list) for i in range(n // 2 - 1, -1, -1): - heapify(unsorted, i, n) + heapify(unsorted_list, i, n) for i in range(n - 1, 0, -1): - unsorted[0], unsorted[i] = unsorted[i], unsorted[0] - heapify(unsorted, 0, i) - return unsorted + unsorted_list[0], unsorted_list[i] = unsorted_list[i], unsorted_list[0] + heapify(unsorted_list, 0, i) + return unsorted_list if __name__ == "__main__": - user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item) for item in user_input.split(",")] - print(heap_sort(unsorted)) + import doctest + + doctest.testmod() From 5c479c0821b70a0ba89da988c4a93f1f52bf9ea5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 01:18:51 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/heap_sort.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 54cfd80913d8..ae75c9c038ac 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -30,7 +30,10 @@ def heapify(unsorted_list: List[int], index: int, heap_size: int) -> None: largest = right_index if largest != index: - unsorted_list[largest], unsorted_list[index] = unsorted_list[index], unsorted_list[largest] + unsorted_list[largest], unsorted_list[index] = ( + unsorted_list[index], + unsorted_list[largest], + ) heapify(unsorted_list, largest, heap_size) From 708146956ee2133d61977eaf4b961a70e7f38d05 Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Sat, 7 Oct 2023 10:29:25 -0300 Subject: [PATCH 3/5] fix typing --- sorts/heap_sort.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index ae75c9c038ac..657546bd6ff9 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -9,10 +9,9 @@ For manual testing run: python heap_sort.py """ -from typing import List -def heapify(unsorted_list: List[int], index: int, heap_size: int) -> None: +def heapify(unsorted_list: list[int], index: int, heap_size: int) -> None: """ :param unsorted_list: unsorted list containing integers numbers @@ -37,7 +36,7 @@ def heapify(unsorted_list: List[int], index: int, heap_size: int) -> None: heapify(unsorted_list, largest, heap_size) -def heap_sort(unsorted_list: List[int]) -> List[int]: +def heap_sort(unsorted_list: list[int]) -> list[int]: """ Pure implementation of the heap sort algorithm in Python From e0027e6c725df0502b8ec400b166d3c7292184ea Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 18:51:41 +0100 Subject: [PATCH 4/5] Update heap_sort.py --- sorts/heap_sort.py | 58 ++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 657546bd6ff9..c6b2efe31be3 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -1,72 +1,70 @@ """ -This is a pure Python implementation of the heap sort algorithm. - -For doctests run following command: -python -m doctest -v heap_sort.py -or -python3 -m doctest -v heap_sort.py - -For manual testing run: -python heap_sort.py +A pure Python implementation of the heap sort algorithm. """ -def heapify(unsorted_list: list[int], index: int, heap_size: int) -> None: +def heapify(unsorted: list[int], index: int, heap_size: int) -> None: """ - - :param unsorted_list: unsorted list containing integers numbers + :param unsorted: unsorted list containing integers numbers :param index: index :param heap_size: size of the heap :return: None + >>> unsorted = [1, 4, 3, 5, 2] + >>> heapify(unsorted, 0, len(unsorted)) + >>> unsorted + [4, 5, 3, 1, 2] + >>> heapify(unsorted, 0, len(unsorted)) + >>> unsorted + [5, 4, 3, 1, 2] """ largest = index left_index = 2 * index + 1 right_index = 2 * index + 2 - if left_index < heap_size and unsorted_list[left_index] > unsorted_list[largest]: + if left_index < heap_size and unsorted[left_index] > unsorted[largest]: largest = left_index - if right_index < heap_size and unsorted_list[right_index] > unsorted_list[largest]: + if right_index < heap_size and unsorted[right_index] > unsorted[largest]: largest = right_index if largest != index: - unsorted_list[largest], unsorted_list[index] = ( - unsorted_list[index], - unsorted_list[largest], + unsorted[largest], unsorted[index] = ( + unsorted[index], + unsorted[largest], ) - heapify(unsorted_list, largest, heap_size) + heapify(unsorted, largest, heap_size) -def heap_sort(unsorted_list: list[int]) -> list[int]: +def heap_sort(unsorted: list[int]) -> list[int]: """ - Pure implementation of the heap sort algorithm in Python + A pure Python implementation of the heap sort algorithm - :param collection: some mutable ordered collection with heterogeneous - comparable items inside + :param collection: a mutable ordered collection of heterogeneous comparable items :return: the same collection ordered by ascending Examples: >>> heap_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - >>> heap_sort([]) [] - >>> heap_sort([-2, -5, -45]) [-45, -5, -2] - >>> heap_sort([3, 7, 9, 28, 123, -5, 8, -30, -200, 0, 4]) [-200, -30, -5, 0, 3, 4, 7, 8, 9, 28, 123] """ - n = len(unsorted_list) + n = len(unsorted) for i in range(n // 2 - 1, -1, -1): - heapify(unsorted_list, i, n) + heapify(unsorted, i, n) for i in range(n - 1, 0, -1): - unsorted_list[0], unsorted_list[i] = unsorted_list[i], unsorted_list[0] - heapify(unsorted_list, 0, i) - return unsorted_list + unsorted[0], unsorted[i] = unsorted[i], unsorted[0] + heapify(unsorted, 0, i) + return unsorted if __name__ == "__main__": import doctest doctest.testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() + if user_input: + unsorted = [int(item) for item in user_input.split(",")] + print(f"{heap_sort(unsorted) = }") From 5c8dcfddec6fa57c7fe97c65b994ecee0739b3ae Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 18:54:03 +0100 Subject: [PATCH 5/5] Update heap_sort.py --- sorts/heap_sort.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index c6b2efe31be3..44ee1d4b39f1 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -27,10 +27,7 @@ def heapify(unsorted: list[int], index: int, heap_size: int) -> None: largest = right_index if largest != index: - unsorted[largest], unsorted[index] = ( - unsorted[index], - unsorted[largest], - ) + unsorted[largest], unsorted[index] = (unsorted[index], unsorted[largest]) heapify(unsorted, largest, heap_size)