Skip to content

Instantly share code, notes, and snippets.

@mmahesh
Last active January 3, 2016 01:39
Show Gist options
  • Save mmahesh/8390237 to your computer and use it in GitHub Desktop.
Save mmahesh/8390237 to your computer and use it in GitHub Desktop.
import unittest
def check_in_range(a):
if a>=0 and a<5:
return True
else:
return False
def find_matrix(blocker):
a = [[1,0,1,1,1],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[1,1,1,0,1]]
if not blocker:
return a
blocker_list = str(blocker).split(';')
for block in blocker_list:
x0 = int(block[1])
y0 = int(block[3])
x1 = int(block[7])
y1 = int(block[9])
if x0==x1:
while y0<=y1:
a[x0][y0]=1
y0=y0+1
elif y0==y1:
while x0<=x1:
a[x0][y0]=1
x0=x0+1
else:
pass
return a
c = raw_input("Give the blocks (Press Enter for default solution): ")
b = find_matrix(str(c))
f_x = 4
f_y = 3
def find_path(x,y,b,sol):
global f_x
global f_y
if not check_in_range(x) and check_in_range(y):
return False
if x == f_x and y == f_y:
sol.append([x,y])
return True
if b[x][y] == 1:
return False
sol.append([x,y])
if find_path(x,y+1,b,sol) == True:
return True
if find_path(x+1,y,b,sol) == True:
return True
if find_path(x,y-1,b,sol) == True:
return True
if find_path(x-1,y,b,sol) == True:
return True
sol.remove([x,y])
return False
def get_solution(b):
sol = []
try:
find_path(0,1,b,sol)
for item in sol:
print item
return sol
except:
return "blocked"
print "Your Output"
get_solution(b)
print "Tests start from here"
a1 = [[1,0,1,1,1],[1,0,0,0,1],[1,1,1,0,1],[1,0,0,0,1],[1,1,1,0,1]]
a2 = [[1,0,1,1,1],[1,0,1,0,1],[1,0,0,0,1],[1,0,1,0,1],[1,1,1,0,1]]
a3 = [[1,0,1,1,1],[1,0,0,0,1],[1,0,1,1,1],[1,0,1,0,1],[1,1,1,0,1]]
class MazeTest(unittest.TestCase):
global a1
global a2
global a3
def test_range(self):
self.assertEqual(check_in_range(2),True)
self.assertEqual(check_in_range(3),True)
self.assertEqual(check_in_range(5),False)
def test_blocker(self):
self.assertEqual(find_matrix('[2,0]:[2,2]'),a1)
self.assertEqual(find_matrix('[0,2]:[1,2];[3,2]:[4,2]'),a2)
self.assertEqual(find_matrix('[2,2]:[2,3];[2,2]:[3,2]'),a3)
def test_find_path(self):
self.assertEqual(find_path(0,1,a1,sol=[]),True)
self.assertEqual(find_path(0,1,a2,sol=[]),True)
def test_get_solution(self):
self.assertEqual(get_solution(a1),[[0, 1], [1, 1], [1, 2], [1, 3], [2, 3], [3, 3], [4, 3]])
self.assertEqual(get_solution(a2),[[0, 1], [1, 1], [2, 1], [2, 2], [2, 3], [3, 3], [4, 3]])
self.assertEqual(get_solution(a3),'blocked')
def test_global_variables(self):
self.assertEqual(f_x,4)
self.assertEqual(f_y,3)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment