From 21db824cc5b6aa695dc47019dfa028a422730873 Mon Sep 17 00:00:00 2001 From: YenChen Date: Tue, 22 Feb 2022 16:50:24 +0100 Subject: [PATCH] test: issue #1 add coverage measurement tool --- sorts/cycle_sort.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py index 806f40441d79..e7955eb95912 100644 --- a/sorts/cycle_sort.py +++ b/sorts/cycle_sort.py @@ -3,6 +3,7 @@ Source: https://en.wikipedia.org/wiki/Cycle_sort """ +branch_reached = set() def cycle_sort(array: list) -> list: """ @@ -18,36 +19,58 @@ def cycle_sort(array: list) -> list: >>> cycle_sort([]) [] """ + global branch_reached + array_len = len(array) for cycle_start in range(0, array_len - 1): + branch_reached.add(0) item = array[cycle_start] pos = cycle_start for i in range(cycle_start + 1, array_len): + branch_reached.add(2) if array[i] < item: + branch_reached.add(3) pos += 1 - + branch_reached.add(16) + + branch_reached.add(4) + if pos == cycle_start: + branch_reached.add(5) continue - + + branch_reached.add(6) + while item == array[pos]: + branch_reached.add(7) pos += 1 + branch_reached.add(8) array[pos], item = item, array[pos] while pos != cycle_start: + branch_reached.add(9) pos = cycle_start for i in range(cycle_start + 1, array_len): + branch_reached.add(10) if array[i] < item: + branch_reached.add(11) pos += 1 + branch_reached.add(12) + branch_reached.add(13) while item == array[pos]: + branch_reached.add(14) pos += 1 - + branch_reached.add(15) array[pos], item = item, array[pos] - + branch_reached.add(17) + branch_reached.add(1) return array if __name__ == "__main__": 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] + print(sorted(branch_reached)) + print(len(branch_reached)/18)