From 69ed756577617f3d2bb893e989ee31239b507b49 Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 27 Aug 2020 19:29:38 +0800 Subject: [PATCH 1/3] Update pigeon_sort.py --- sorts/pigeon_sort.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index cc6205f804dc..c2435c2d3b60 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -26,29 +26,18 @@ def pigeon_sort(array): if len(array) == 0: return array - # Manually finds the minimum and maximum of the array. - min = array[0] - max = array[0] - - for i in range(len(array)): - if array[i] < min: - min = array[i] - elif array[i] > max: - max = array[i] + # Finds the minimum and maximum of the array. + _min, _max = min(array), max(array) # Compute the variables - holes_range = max - min + 1 - holes = [0 for _ in range(holes_range)] - holes_repeat = [0 for _ in range(holes_range)] + holes_range = _max - _min + 1 + holes, holes_repeat = [0]*holes_range, [0]*holes_range # Make the sorting. - for i in range(len(array)): - index = array[i] - min - if holes[index] != array[i]: - holes[index] = array[i] - holes_repeat[index] += 1 - else: - holes_repeat[index] += 1 + for i in array: + index = i - _min + holes[index] = i + holes_repeat[index] += 1 # Makes the array back by replacing the numbers. index = 0 @@ -63,6 +52,8 @@ def pigeon_sort(array): if __name__ == "__main__": + import doctest + doctest.testmod() user_input = input("Enter numbers separated by comma:\n") unsorted = [int(x) for x in user_input.split(",")] print(pigeon_sort(unsorted)) From 3fdc8320a1a30ce4576c99ca3f771985a85d046d Mon Sep 17 00:00:00 2001 From: wuyudi Date: Thu, 27 Aug 2020 20:45:27 +0800 Subject: [PATCH 2/3] Update pigeon_sort.py --- sorts/pigeon_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index c2435c2d3b60..5c90917fe278 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -31,7 +31,7 @@ def pigeon_sort(array): # Compute the variables holes_range = _max - _min + 1 - holes, holes_repeat = [0]*holes_range, [0]*holes_range + holes, holes_repeat = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: From d2b8b10d201151e42ab7338b626433d3e2c8f3ac Mon Sep 17 00:00:00 2001 From: wuyudi Date: Sat, 26 Sep 2020 11:41:10 +0800 Subject: [PATCH 3/3] Update pigeon_sort.py --- sorts/pigeon_sort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py index 5c90917fe278..3126e47c719e 100644 --- a/sorts/pigeon_sort.py +++ b/sorts/pigeon_sort.py @@ -26,7 +26,6 @@ def pigeon_sort(array): if len(array) == 0: return array - # Finds the minimum and maximum of the array. _min, _max = min(array), max(array) # Compute the variables