From ca5a802e2603c81b259724acdb015ca0f5da154a Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 17 Aug 2020 20:58:16 +0200 Subject: [PATCH 1/2] insertion sort : docstring, type hinting --- sorts/insertion_sort.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index ca678381b431..357cb3049337 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -1,9 +1,12 @@ """ This is a pure Python implementation of the insertion sort algorithm +This algorithm sorts a collection by comparing adjacent elements. +When it finds that order is not respected, it moves the element compared +backward until order is right. +It then goes back directly to element initial position resuming forward comparison. + For doctests run following command: -python -m doctest -v insertion_sort.py -or python3 -m doctest -v insertion_sort.py For manual testing run: @@ -11,7 +14,7 @@ """ -def insertion_sort(collection): +def insertion_sort(collection: list) -> list: """Pure implementation of the insertion sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous From 125859d181be3ac48d75dbc07c01092f2bab07ab Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 23 Aug 2020 04:29:09 +0200 Subject: [PATCH 2/2] Update insertion_sort.py --- sorts/insertion_sort.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index 357cb3049337..28458ad1b86d 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -1,21 +1,21 @@ """ -This is a pure Python implementation of the insertion sort algorithm +A pure Python implementation of the insertion sort algorithm This algorithm sorts a collection by comparing adjacent elements. When it finds that order is not respected, it moves the element compared -backward until order is right. -It then goes back directly to element initial position resuming forward comparison. +backward until the order is correct. It then goes back directly to the +element's initial position resuming forward comparison. For doctests run following command: python3 -m doctest -v insertion_sort.py For manual testing run: -python insertion_sort.py +python3 insertion_sort.py """ def insertion_sort(collection: list) -> list: - """Pure implementation of the insertion sort algorithm in Python + """A pure Python implementation of the insertion sort algorithm :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -50,4 +50,4 @@ def insertion_sort(collection: list) -> 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(insertion_sort(unsorted)) + print(f"{insertion_sort(unsorted) = }")