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 <johnlaw.po@gmail.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
mateuszz0000 2020-06-02 11:51:22 +02:00 committed by GitHub
parent dc720a83d7
commit b080a5e027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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. 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): for i in range(len(unsorted) - 1, 0, -1):
swapped = False swapped = False
@ -20,7 +37,9 @@ def cocktail_shaker_sort(unsorted):
if __name__ == "__main__": if __name__ == "__main__":
import doctest
doctest.testmod()
user_input = input("Enter numbers separated by a comma:\n").strip() user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")] unsorted = [int(item) for item in user_input.split(",")]
cocktail_shaker_sort(unsorted) print(f"{cocktail_shaker_sort(unsorted) = }")
print(unsorted)