From e94ae81d2f6dd66476b2066464317b49cda1f603 Mon Sep 17 00:00:00 2001 From: Your name hereclear Date: Tue, 21 Feb 2023 09:38:36 +0100 Subject: [PATCH 1/3] Feature - Coverage check for maze_search #14 --- algorithms/bfs/maze_search.py | 40 +++++++++- algorithms/linkedlist/intersection.py | 108 ++++++++++++++++---------- 2 files changed, 105 insertions(+), 43 deletions(-) diff --git a/algorithms/bfs/maze_search.py b/algorithms/bfs/maze_search.py index f7410d46e..6160602a1 100644 --- a/algorithms/bfs/maze_search.py +++ b/algorithms/bfs/maze_search.py @@ -27,14 +27,21 @@ ''' def maze_search(maze): + flags = [False for i in range(10)] BLOCKED, ALLOWED = 0, 1 UNVISITED, VISITED = 0, 1 initial_x, initial_y = 0, 0 if maze[initial_x][initial_y] == BLOCKED: + flags[0] = True + print(flags) + print(len(list(filter(lambda x:(x == True) , flags))) / len(flags)) return -1 + else: + flags[1] = True + directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] height, width = len(maze), len(maze[0]) @@ -47,21 +54,48 @@ def maze_search(maze): is_visited[initial_x][initial_y] = VISITED while queue: + flags[2] = True x, y, steps = queue.popleft() if x == target_x and y == target_y: + flags[3] = True + print(flags) + print(len(list(filter(lambda x:(x == True) , flags))) / len(flags)) return steps - + else: + flags[4] = True for dx, dy in directions: + flags[5] = True new_x = x + dx new_y = y + dy - if not (0 <= new_x < height and 0 <= new_y < width): + flags[6] = True continue + else: + flags[7] if maze[new_x][new_y] == ALLOWED and is_visited[new_x][new_y] == UNVISITED: + flags[8] = True queue.append((new_x, new_y, steps + 1)) is_visited[new_x][new_y] = VISITED - + else: + flags[9] = True + print(len(list(filter(lambda x:(x == True) , flags))) / len(flags)) + print(flags) return -1 +if __name__ == "__main__": + print("Coverages for ex1") + ex1 = [[1,0,0], + [0,1,1], + [0,1,1]] + maze_search(ex1) + print("Coverages for ex2") + ex2 = [[1,0,1,1,1,1], + [1,0,1,0,1,0], + [1,0,1,0,1,1], + [1,1,1,0,1,1]] + maze_search(ex2) + + + \ No newline at end of file diff --git a/algorithms/linkedlist/intersection.py b/algorithms/linkedlist/intersection.py index 71c4c1c61..7643d0e8e 100644 --- a/algorithms/linkedlist/intersection.py +++ b/algorithms/linkedlist/intersection.py @@ -12,6 +12,8 @@ import unittest + + class Node(object): def __init__(self, val=None): self.val = val @@ -19,83 +21,109 @@ def __init__(self, val=None): def intersection(h1, h2): + flags = [False for i in range(9)] count = 0 flag = None h1_orig = h1 h2_orig = h2 - while h1 or h2: + while h1 or h2: ## 1 + flags[0] = True count += 1 - if not flag and (h1.next is None or h2.next is None): + if not flag and (h1.next is None or h2.next is None): ## 3 + flags[1] = True # We hit the end of one of the lists, set a flag for this flag = (count, h1.next, h2.next) - if h1: + if h1: #5 + flags[2] = True h1 = h1.next - if h2: + if h2: #6 + flags[3] = True h2 = h2.next long_len = count # Mark the length of the longer of the two lists short_len = flag[0] - if flag[1] is None: + if flag[1] is None: #7 + flags[4] = True shorter = h1_orig longer = h2_orig - elif flag[2] is None: + elif flag[2] is None: #8 + flags[5] = True shorter = h2_orig longer = h1_orig - while longer and shorter: + while longer and shorter: #9 + flags[6] = True - while long_len > short_len: + while long_len > short_len: #10 + flags[7] = True # force the longer of the two lists to "catch up" longer = longer.next long_len -= 1 - if longer == shorter: - # The nodes match, return the node - return longer - else: + if longer == shorter: #11 + flags[7] = True + + + print(len(list(filter(lambda x:(x == True) , flags))) / len(flags)) + + + + return longer ##12 + else: ## 13 + flags[8] = True longer = longer.next shorter = shorter.next - + print(flags) + # branch_coverage = len(flags) / len(filter(lambda x:(x == True) , flags)) + # print(branch_coverage) return None -class TestSuite(unittest.TestCase): - def test_intersection(self): - # create linked list as: - # 1 -> 3 -> 5 - # \ - # 7 -> 9 -> 11 - # / - # 2 -> 4 -> 6 - a1 = Node(1) - b1 = Node(3) - c1 = Node(5) - d = Node(7) - a2 = Node(2) - b2 = Node(4) - c2 = Node(6) - e = Node(9) - f = Node(11) + + +def test_intersection(): - a1.next = b1 - b1.next = c1 - c1.next = d - a2.next = b2 - b2.next = c2 - c2.next = d - d.next = e - e.next = f + # create linked list as: + # 1 -> 3 -> 5 + # \ + # 7 -> 9 -> 11 + # / + # 2 -> 4 -> 6 + a1 = Node(1) + b1 = Node(3) + c1 = Node(5) + d = Node(7) + a2 = Node(2) + b2 = Node(4) + c2 = Node(6) + e = Node(9) + f = Node(11) - self.assertEqual(7, intersection(a1, a2).val) + a1.next = d + b1.next = d + c1.next = d + a2.next = d + b2.next = d + c2.next = d + d.next = d + e.next = d + intersection(a1, a2) + + if __name__ == '__main__': - unittest.main() + + + test_intersection() + + + # unittest.main() From 56a5bcc47607cb060f1cf24c393f0fd1dc4e5fe6 Mon Sep 17 00:00:00 2001 From: Your name hereclear Date: Tue, 21 Feb 2023 09:44:41 +0100 Subject: [PATCH 2/3] Feature - Improve coverage for maze_search #15 --- algorithms/bfs/maze_search.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/algorithms/bfs/maze_search.py b/algorithms/bfs/maze_search.py index 6160602a1..315b45e34 100644 --- a/algorithms/bfs/maze_search.py +++ b/algorithms/bfs/maze_search.py @@ -96,6 +96,9 @@ def maze_search(maze): [1,0,1,0,1,1], [1,1,1,0,1,1]] maze_search(ex2) + print("Coverages for ex3") + ex3 = [[0,0,0,0], [0,0,0,0],[0,0,0,0],[0,0,0,0]] + maze_search(ex3) \ No newline at end of file From 6f46a61da046f08803ca45072582dee2a86394a8 Mon Sep 17 00:00:00 2001 From: Your name hereclear Date: Tue, 21 Feb 2023 10:39:59 +0100 Subject: [PATCH 3/3] Fix - Flags not set bug for coverage - #17 --- algorithms/bfs/maze_search.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/algorithms/bfs/maze_search.py b/algorithms/bfs/maze_search.py index 315b45e34..11a72fef4 100644 --- a/algorithms/bfs/maze_search.py +++ b/algorithms/bfs/maze_search.py @@ -64,6 +64,7 @@ def maze_search(maze): return steps else: flags[4] = True + for dx, dy in directions: flags[5] = True new_x = x + dx @@ -72,7 +73,7 @@ def maze_search(maze): flags[6] = True continue else: - flags[7] + flags[7] = True if maze[new_x][new_y] == ALLOWED and is_visited[new_x][new_y] == UNVISITED: flags[8] = True @@ -84,6 +85,8 @@ def maze_search(maze): print(flags) return -1 + + if __name__ == "__main__": print("Coverages for ex1") ex1 = [[1,0,0], @@ -99,6 +102,11 @@ def maze_search(maze): print("Coverages for ex3") ex3 = [[0,0,0,0], [0,0,0,0],[0,0,0,0],[0,0,0,0]] maze_search(ex3) + print("Coverages for ex4") + ex4 = [[0],[0],[0]] + maze_search(ex4) + + \ No newline at end of file