From c4115c5e3ddacb27c60245c37b92336759bc25a5 Mon Sep 17 00:00:00 2001 From: YenChen Date: Tue, 22 Feb 2022 15:55:17 +0100 Subject: [PATCH] test: issue #2 add 2 unit tests to cycle_sort() --- sorts/cycle_sort.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py index 806f40441d79..182e2061c2c5 100644 --- a/sorts/cycle_sort.py +++ b/sorts/cycle_sort.py @@ -8,13 +8,10 @@ def cycle_sort(array: list) -> list: """ >>> cycle_sort([4, 3, 2, 1]) [1, 2, 3, 4] - >>> cycle_sort([-4, 20, 0, -50, 100, -1]) [-50, -4, -1, 0, 20, 100] - >>> cycle_sort([-.1, -.2, 1.3, -.8]) [-0.8, -0.2, -0.1, 1.3] - >>> cycle_sort([]) [] """ @@ -29,7 +26,12 @@ def cycle_sort(array: list) -> list: if pos == cycle_start: continue - + + """ + the place that the current element should be + switched to are already occupied by element with + same value + """ while item == array[pos]: pos += 1 @@ -39,7 +41,11 @@ def cycle_sort(array: list) -> list: for i in range(cycle_start + 1, array_len): if array[i] < item: pos += 1 - + """ + In the second turn of switching, the place that + the current element should be switched to are already + occupied by element with same value + """ while item == array[pos]: pos += 1 @@ -49,5 +55,7 @@ def cycle_sort(array: list) -> list: if __name__ == "__main__": + assert cycle_sort([3, 3, 3, 3, 2]) == [2, 3, 3, 3, 3] + assert cycle_sort([3, 2, 3, 3, 3, 2, 1]) == [1, 2, 2, 3, 3, 3, 3] assert cycle_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] - assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15] + assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15] \ No newline at end of file