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
Changes from 1 commit
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
Next Next commit
doctests and intro docstring added
  • Loading branch information
parth-paradkar committed Oct 3, 2019
commit 3b2223ffe9e733726f501ccbd5189535f0abeb54
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:
python -m doctest -v pancake_sort.py
or
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.

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))