From 3b2223ffe9e733726f501ccbd5189535f0abeb54 Mon Sep 17 00:00:00 2001 From: Parth Paradkar Date: Thu, 3 Oct 2019 13:04:01 +0530 Subject: [PATCH 1/4] doctests and intro docstring added --- sorts/pancake_sort.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/sorts/pancake_sort.py b/sorts/pancake_sort.py index 3b48bc6e46d9..8cd38c02081b 100644 --- a/sorts/pancake_sort.py +++ b/sorts/pancake_sort.py @@ -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 +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 @@ -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)) From 9c59950eee7eae5ec4bbc3c08167b91772ed338c Mon Sep 17 00:00:00 2001 From: Parth Paradkar Date: Thu, 3 Oct 2019 13:10:22 +0530 Subject: [PATCH 2/4] doctests, docstrings and check for empty collection added --- sorts/pigeon_sort.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index 5e5afa137685..e55b6c159c05 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -2,6 +2,20 @@ This is an implementation of Pigeon Hole Sort. ''' 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] @@ -37,6 +51,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)) From 329f9780cd753bf5d66ad38cf1e92e2e45825ec2 Mon Sep 17 00:00:00 2001 From: Parth Paradkar Date: Thu, 3 Oct 2019 13:33:12 +0530 Subject: [PATCH 3/4] Intro docstring added --- sorts/pigeon_sort.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index e55b6c159c05..ad5591e4e300 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -1,5 +1,12 @@ ''' This is an implementation of Pigeon Hole Sort. + For doctests run following command: + python -m doctest -v pigeon_sort.py + or + python3 -m doctest -v pigeon_sort.py + + For manual testing run: + python pigeon_sort.py ''' def pigeon_sort(array): """ From 7c695f6d09acb9fb9d04c2d81c393e9acaac628d Mon Sep 17 00:00:00 2001 From: Parth Paradkar Date: Thu, 3 Oct 2019 13:45:30 +0530 Subject: [PATCH 4/4] python versions reversed --- sorts/pancake_sort.py | 4 ++-- sorts/pigeon_sort.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sorts/pancake_sort.py b/sorts/pancake_sort.py index 8cd38c02081b..873c14a0a174 100644 --- a/sorts/pancake_sort.py +++ b/sorts/pancake_sort.py @@ -1,9 +1,9 @@ """ 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 +or +python -m doctest -v pancake_sort.py For manual testing run: python pancake_sort.py """ diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index ad5591e4e300..5417234d331b 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -1,10 +1,11 @@ ''' This is an implementation of Pigeon Hole Sort. For doctests run following command: - python -m doctest -v pigeon_sort.py - or + python3 -m doctest -v pigeon_sort.py - + or + python -m doctest -v pigeon_sort.py + For manual testing run: python pigeon_sort.py '''