File tree 1 file changed +9
-19
lines changed
1 file changed +9
-19
lines changed Original file line number Diff line number Diff line change @@ -26,29 +26,17 @@ def pigeon_sort(array):
26
26
if len (array ) == 0 :
27
27
return array
28
28
29
- # Manually finds the minimum and maximum of the array.
30
- min = array [0 ]
31
- max = array [0 ]
32
-
33
- for i in range (len (array )):
34
- if array [i ] < min :
35
- min = array [i ]
36
- elif array [i ] > max :
37
- max = array [i ]
29
+ _min , _max = min (array ), max (array )
38
30
39
31
# Compute the variables
40
- holes_range = max - min + 1
41
- holes = [0 for _ in range (holes_range )]
42
- holes_repeat = [0 for _ in range (holes_range )]
32
+ holes_range = _max - _min + 1
33
+ holes , holes_repeat = [0 ] * holes_range , [0 ] * holes_range
43
34
44
35
# Make the sorting.
45
- for i in range (len (array )):
46
- index = array [i ] - min
47
- if holes [index ] != array [i ]:
48
- holes [index ] = array [i ]
49
- holes_repeat [index ] += 1
50
- else :
51
- holes_repeat [index ] += 1
36
+ for i in array :
37
+ index = i - _min
38
+ holes [index ] = i
39
+ holes_repeat [index ] += 1
52
40
53
41
# Makes the array back by replacing the numbers.
54
42
index = 0
@@ -63,6 +51,8 @@ def pigeon_sort(array):
63
51
64
52
65
53
if __name__ == "__main__" :
54
+ import doctest
55
+ doctest .testmod ()
66
56
user_input = input ("Enter numbers separated by comma:\n " )
67
57
unsorted = [int (x ) for x in user_input .split ("," )]
68
58
print (pigeon_sort (unsorted ))
You can’t perform that action at this time.
0 commit comments