Skip to content

Commit b080a5e

Browse files
l3str4ngepoyeacclauss
authored
Doctests + typehints in cocktail shaker sort (TheAlgorithms#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>
1 parent dc720a8 commit b080a5e

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

sorts/cocktail_shaker_sort.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
def cocktail_shaker_sort(unsorted):
1+
""" https://en.wikipedia.org/wiki/Cocktail_shaker_sort """
2+
3+
4+
def cocktail_shaker_sort(unsorted: list) -> list:
25
"""
36
Pure implementation of the cocktail shaker sort algorithm in Python.
7+
>>> cocktail_shaker_sort([4, 5, 2, 1, 2])
8+
[1, 2, 2, 4, 5]
9+
10+
>>> cocktail_shaker_sort([-4, 5, 0, 1, 2, 11])
11+
[-4, 0, 1, 2, 5, 11]
12+
13+
>>> cocktail_shaker_sort([0.1, -2.4, 4.4, 2.2])
14+
[-2.4, 0.1, 2.2, 4.4]
15+
16+
>>> cocktail_shaker_sort([1, 2, 3, 4, 5])
17+
[1, 2, 3, 4, 5]
18+
19+
>>> cocktail_shaker_sort([-4, -5, -24, -7, -11])
20+
[-24, -11, -7, -5, -4]
421
"""
522
for i in range(len(unsorted) - 1, 0, -1):
623
swapped = False
@@ -20,7 +37,9 @@ def cocktail_shaker_sort(unsorted):
2037

2138

2239
if __name__ == "__main__":
40+
import doctest
41+
42+
doctest.testmod()
2343
user_input = input("Enter numbers separated by a comma:\n").strip()
2444
unsorted = [int(item) for item in user_input.split(",")]
25-
cocktail_shaker_sort(unsorted)
26-
print(unsorted)
45+
print(f"{cocktail_shaker_sort(unsorted) = }")

0 commit comments

Comments
 (0)