Skip to content

Commit

Permalink
Feature - Coverage check for maze_search #14
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasche153 committed Feb 21, 2023
1 parent 52b9408 commit e94ae81
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 43 deletions.
40 changes: 37 additions & 3 deletions algorithms/bfs/maze_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)



108 changes: 68 additions & 40 deletions algorithms/linkedlist/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,90 +12,118 @@
import unittest




class Node(object):
def __init__(self, val=None):
self.val = val
self.next = 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()

0 comments on commit e94ae81

Please sign in to comment.