From 976e79f36f74700f604c55bcf538b76a39376675 Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Tue, 2 Jun 2020 08:06:01 +0200 Subject: [PATCH 1/3] Doctests in cocktail shaker sort --- sorts/cocktail_shaker_sort.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index ab624421a3d6..e2ca14385791 100644 --- a/sorts/cocktail_shaker_sort.py +++ b/sorts/cocktail_shaker_sort.py @@ -1,6 +1,23 @@ -def cocktail_shaker_sort(unsorted): +""" https://en.wikipedia.org/wiki/Cocktail_shaker_sort """ + + +def cocktail_shaker_sort(unsorted: list) -> list: """ Pure implementation of the cocktail shaker sort algorithm in Python. + >>> cocktail_shaker_sort([4, 5, 2, 1, 2]) + [1, 2, 2, 4, 5] + + >>> cocktail_shaker_sort([-4, 5, 0, 1, 2, 11]) + [-4, 0, 1, 2, 5, 11] + + >>> cocktail_shaker_sort([0.1, -2.4, 4.4, 2.2]) + [-2.4, 0.1, 2.2, 4.4] + + >>> cocktail_shaker_sort([1, 2, 3, 4, 5]) + [1, 2, 3, 4, 5] + + >>> cocktail_shaker_sort([-4, -5, -24, -7, -11]) + [-24, -11, -7, -5, -4] """ for i in range(len(unsorted) - 1, 0, -1): swapped = False @@ -22,5 +39,4 @@ def cocktail_shaker_sort(unsorted): if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - cocktail_shaker_sort(unsorted) - print(unsorted) + print(f"Unsorted: {unsorted}. Sorted: {cocktail_shaker_sort(unsorted)}") From eec8b5f78728b9fa3b12d983a7a807167197aff0 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 2 Jun 2020 11:47:07 +0200 Subject: [PATCH 2/3] import doctest --- sorts/cocktail_shaker_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index e2ca14385791..b5cdfe1abb93 100644 --- a/sorts/cocktail_shaker_sort.py +++ b/sorts/cocktail_shaker_sort.py @@ -37,6 +37,8 @@ def cocktail_shaker_sort(unsorted: list) -> list: if __name__ == "__main__": + import doctest + doctest.testmod() user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] print(f"Unsorted: {unsorted}. Sorted: {cocktail_shaker_sort(unsorted)}") From 829e8fa69c2adfa22214d87da9cd995435492582 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 2 Jun 2020 11:50:58 +0200 Subject: [PATCH 3/3] print(f"{cocktail_shaker_sort(unsorted) = }") --- sorts/cocktail_shaker_sort.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index b5cdfe1abb93..42015abc5f97 100644 --- a/sorts/cocktail_shaker_sort.py +++ b/sorts/cocktail_shaker_sort.py @@ -38,7 +38,8 @@ def cocktail_shaker_sort(unsorted: list) -> list: if __name__ == "__main__": import doctest + doctest.testmod() user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(f"Unsorted: {unsorted}. Sorted: {cocktail_shaker_sort(unsorted)}") + print(f"{cocktail_shaker_sort(unsorted) = }")