Skip to content

Commit 390feb0

Browse files
parth-paradkarcclauss
authored andcommitted
Add doctests for sorting algorithms (#1263)
* doctests and intro docstring added * doctests, docstrings and check for empty collection added * Intro docstring added * python versions reversed
1 parent b8490ed commit 390feb0

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

Diff for: sorts/pancake_sort.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
"""Pancake Sort Algorithm."""
2-
# Only can reverse array from 0 to i
3-
1+
"""
2+
This is a pure python implementation of the pancake sort algorithm
3+
For doctests run following command:
4+
python3 -m doctest -v pancake_sort.py
5+
or
6+
python -m doctest -v pancake_sort.py
7+
For manual testing run:
8+
python pancake_sort.py
9+
"""
410

511
def pancake_sort(arr):
6-
"""Sort Array with Pancake Sort."""
12+
"""Sort Array with Pancake Sort.
13+
:param arr: Collection containing comparable items
14+
:return: Collection ordered in ascending order of items
15+
Examples:
16+
>>> pancake_sort([0, 5, 3, 2, 2])
17+
[0, 2, 2, 3, 5]
18+
>>> pancake_sort([])
19+
[]
20+
>>> pancake_sort([-2, -5, -45])
21+
[-45, -5, -2]
22+
"""
723
cur = len(arr)
824
while cur > 1:
925
# Find the maximum number in arr
@@ -17,4 +33,6 @@ def pancake_sort(arr):
1733

1834

1935
if __name__ == '__main__':
20-
print(pancake_sort([0, 10, 15, 3, 2, 9, 14, 13]))
36+
user_input = input('Enter numbers separated by a comma:\n').strip()
37+
unsorted = [int(item) for item in user_input.split(',')]
38+
print(pancake_sort(unsorted))

Diff for: sorts/pigeon_sort.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
'''
22
This is an implementation of Pigeon Hole Sort.
3+
For doctests run following command:
4+
5+
python3 -m doctest -v pigeon_sort.py
6+
or
7+
python -m doctest -v pigeon_sort.py
8+
9+
For manual testing run:
10+
python pigeon_sort.py
311
'''
412
def pigeon_sort(array):
13+
"""
14+
Implementation of pigeon hole sort algorithm
15+
:param array: Collection of comparable items
16+
:return: Collection sorted in ascending order
17+
>>> pigeon_sort([0, 5, 3, 2, 2])
18+
[0, 2, 2, 3, 5]
19+
>>> pigeon_sort([])
20+
[]
21+
>>> pigeon_sort([-2, -5, -45])
22+
[-45, -5, -2]
23+
"""
24+
if(len(array) == 0):
25+
return array
26+
527
# Manually finds the minimum and maximum of the array.
628
min = array[0]
729
max = array[0]
@@ -37,6 +59,4 @@ def pigeon_sort(array):
3759
if __name__ == '__main__':
3860
user_input = input('Enter numbers separated by comma:\n')
3961
unsorted = [int(x) for x in user_input.split(',')]
40-
sorted = pigeon_sort(unsorted)
41-
42-
print(sorted)
62+
print(pigeon_sort(unsorted))

0 commit comments

Comments
 (0)