Skip to content

Commit e0ff07a

Browse files
authored
Merge pull request #3 from b06902047/coverage_improvement
test: issue #2 add 2 unit tests to cycle_sort()
2 parents 7a9b3c7 + c4115c5 commit e0ff07a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

sorts/cycle_sort.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ def cycle_sort(array: list) -> list:
88
"""
99
>>> cycle_sort([4, 3, 2, 1])
1010
[1, 2, 3, 4]
11-
1211
>>> cycle_sort([-4, 20, 0, -50, 100, -1])
1312
[-50, -4, -1, 0, 20, 100]
14-
1513
>>> cycle_sort([-.1, -.2, 1.3, -.8])
1614
[-0.8, -0.2, -0.1, 1.3]
17-
1815
>>> cycle_sort([])
1916
[]
2017
"""
@@ -29,7 +26,12 @@ def cycle_sort(array: list) -> list:
2926

3027
if pos == cycle_start:
3128
continue
32-
29+
30+
"""
31+
the place that the current element should be
32+
switched to are already occupied by element with
33+
same value
34+
"""
3335
while item == array[pos]:
3436
pos += 1
3537

@@ -39,7 +41,11 @@ def cycle_sort(array: list) -> list:
3941
for i in range(cycle_start + 1, array_len):
4042
if array[i] < item:
4143
pos += 1
42-
44+
"""
45+
In the second turn of switching, the place that
46+
the current element should be switched to are already
47+
occupied by element with same value
48+
"""
4349
while item == array[pos]:
4450
pos += 1
4551

@@ -49,5 +55,7 @@ def cycle_sort(array: list) -> list:
4955

5056

5157
if __name__ == "__main__":
58+
assert cycle_sort([3, 3, 3, 3, 2]) == [2, 3, 3, 3, 3]
59+
assert cycle_sort([3, 2, 3, 3, 3, 2, 1]) == [1, 2, 2, 3, 3, 3, 3]
5260
assert cycle_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
53-
assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
61+
assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]

0 commit comments

Comments
 (0)