From b080a5e027666919207b215933aa3306e1c57587 Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Tue, 2 Jun 2020 11:51:22 +0200 Subject: [PATCH] Doctests + typehints in cocktail shaker sort (#2061) * Doctests in cocktail shaker sort * import doctest * print(f"{cocktail_shaker_sort(unsorted) = }") Co-authored-by: John Law Co-authored-by: Christian Clauss --- sorts/cocktail_shaker_sort.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index ab624421a..42015abc5 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 @@ -20,7 +37,9 @@ def cocktail_shaker_sort(unsorted): 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(",")] - cocktail_shaker_sort(unsorted) - print(unsorted) + print(f"{cocktail_shaker_sort(unsorted) = }")