Skip to content

Add doctests for sorting algorithms #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions sorts/pancake_sort.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
"""Pancake Sort Algorithm."""
# Only can reverse array from 0 to i

"""
This is a pure python implementation of the pancake sort algorithm
For doctests run following command:
python3 -m doctest -v pancake_sort.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reverse the order to encourage users to try python3 first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sure.

or
python -m doctest -v pancake_sort.py
For manual testing run:
python pancake_sort.py
"""

def pancake_sort(arr):
"""Sort Array with Pancake Sort."""
"""Sort Array with Pancake Sort.
:param arr: Collection containing comparable items
:return: Collection ordered in ascending order of items
Examples:
>>> pancake_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> pancake_sort([])
[]
>>> pancake_sort([-2, -5, -45])
[-45, -5, -2]
"""
cur = len(arr)
while cur > 1:
# Find the maximum number in arr
Expand All @@ -17,4 +33,6 @@ def pancake_sort(arr):


if __name__ == '__main__':
print(pancake_sort([0, 10, 15, 3, 2, 9, 14, 13]))
user_input = input('Enter numbers separated by a comma:\n').strip()
unsorted = [int(item) for item in user_input.split(',')]
print(pancake_sort(unsorted))
26 changes: 23 additions & 3 deletions sorts/pigeon_sort.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
'''
This is an implementation of Pigeon Hole Sort.
For doctests run following command:

python3 -m doctest -v pigeon_sort.py
or
python -m doctest -v pigeon_sort.py

For manual testing run:
python pigeon_sort.py
'''
def pigeon_sort(array):
"""
Implementation of pigeon hole sort algorithm
:param array: Collection of comparable items
:return: Collection sorted in ascending order
>>> pigeon_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> pigeon_sort([])
[]
>>> pigeon_sort([-2, -5, -45])
[-45, -5, -2]
"""
if(len(array) == 0):
return array

# Manually finds the minimum and maximum of the array.
min = array[0]
max = array[0]
Expand Down Expand Up @@ -37,6 +59,4 @@ def pigeon_sort(array):
if __name__ == '__main__':
user_input = input('Enter numbers separated by comma:\n')
unsorted = [int(x) for x in user_input.split(',')]
sorted = pigeon_sort(unsorted)

print(sorted)
print(pigeon_sort(unsorted))