How to use is_connected method in lisa

Best Python code snippet using lisa_python

test_connectivity.py

Source:test_connectivity.py Github

copy

Full Screen

...116 component1 = next(components)117 with pytest.raises(StopIteration):118 next(components)119 assert component1 == set([0, 1, 2, 3, 4, 5])120def test_is_connected():121 # directed122 g = create_graph(123 directed=True,124 allowing_self_loops=False,125 allowing_multiple_edges=False,126 weighted=True,127 )128 g.add_vertices_from([0, 1])129 g.add_edge(0, 1)130 is_connected, components = connectivity.is_connected(g)131 assert not is_connected132 g.add_edge(1, 0)133 is_connected, components = connectivity.is_connected(g)134 assert is_connected135 # undirected136 g = create_graph(137 directed=False,138 allowing_self_loops=False,139 allowing_multiple_edges=False,140 weighted=True,141 )142 g.add_vertices_from([0, 1])143 g.add_edge(0, 1)144 is_connected, components = connectivity.is_connected(g)145 assert is_connected146def test_anyhashableg_strongly_gabow():147 g = create_graph(148 directed=True,149 allowing_self_loops=False,150 allowing_multiple_edges=False,151 weighted=True,152 any_hashable=True,153 )154 g.add_vertices_from(["0", "1", "2", "3", "4", "5"])155 g.add_edge("0", "1")156 g.add_edge("1", "2")157 g.add_edge("2", "0")158 g.add_edge("2", "3")...

Full Screen

Full Screen

HitBricks.py

Source:HitBricks.py Github

copy

Full Screen

1import collections2class Solution(object):3 def get_connected(self, grid, x, y, n, m, is_connected):4 stack, result = [(x,y)], []5 visited = set([(x, y)])6 7 while len(stack) > 0:8 u, v = stack.pop()9 result.append((u,v))10 11 if u == 0 or ((u,v) in is_connected and is_connected[(u,v)]):12 return True, result13 14 for i in [-1,1]:15 if 0 <= u+i < n and 0 <= v < m and grid[u+i][v] == 1 and (u+i, v) not in visited:16 stack.append((u+i,v))17 visited.add((u+i,v))18 19 if 0 <= u < n and 0 <= v+i < m and grid[u][v+i] == 1 and (u, v+i) not in visited:20 stack.append((u,v+i))21 visited.add((u,v+i))22 23 return False, result24 25 26 def get_deleted_neighbors(self, grid, x, y, n, m, is_connected):27 stack, result = [(x,y)], []28 visited = set([(x, y)])29 30 while len(stack) > 0:31 u, v = stack.pop()32 result.append((u,v))33 34 for i in [-1,1]:35 if 0 <= u+i < n and 0 <= v < m and (grid[u+i][v] == 2 or (grid[u+i][v] == 1 and is_connected[(u+i,v)] is False)) and (u+i, v) not in visited:36 stack.append((u+i,v))37 visited.add((u+i,v))38 39 if 0 <= u < n and 0 <= v+i < m and (grid[u][v+i] == 2 or (grid[u][v+i] == 1 and is_connected[(u,v+i)] is False)) and (u, v+i) not in visited:40 stack.append((u,v+i))41 visited.add((u,v+i))42 43 return result44 45 46 def drop_bricks(self, grid, x, y, n, m, is_connected):47 if 0 <= x < n and 0 <= y < m and grid[x][y] == 1:48 connected, result = self.get_connected(grid, x, y, n, m, is_connected)49 for x, y in result:50 if connected is False:51 grid[x][y] = 252 is_connected[(x,y)] = connected53 54 55 def hitBricks(self, grid, hits):56 n, m = len(grid), len(grid[0])57 is_connected = dict()58 59 for x in range(len(grid)):60 for y in range(len(grid[0])):61 if grid[x][y] == 1:62 is_connected[(x,y)] = False63 64 for x, y in hits:65 if grid[x][y] == 1:66 grid[x][y] = -167 68 for x, y in hits:69 if grid[x][y] == -1:70 self.drop_bricks(grid, x+1, y, n, m, is_connected)71 self.drop_bricks(grid, x-1, y, n, m, is_connected)72 self.drop_bricks(grid, x, y+1, n, m, is_connected)73 self.drop_bricks(grid, x, y-1, n, m, is_connected)74 75 out, disconnected_hits = [0]*len(hits), []76 77 for i in reversed(range(len(hits))):78 x, y = hits[i]79 if grid[x][y] == -1:80 grid[x][y] = 181 82 connected, _ = self.get_connected(grid, x, y, n, m, is_connected)83 is_connected[(x,y)] = connected84 85 if connected:86 deleted_neighbors = self.get_deleted_neighbors(grid, x, y, n, m, is_connected)87 cnts = len(deleted_neighbors)-188 for u, v in deleted_neighbors:89 grid[u][v] = 190 is_connected[(u,v)] = connected91 out[i] = cnts92 else:93 disconnected_hits.append(i)94 95 for i in disconnected_hits:96 x, y = hits[i]97 if is_connected[(x,y)] is False:98 grid[x][y] = 199 100 deleted_neighbors = self.get_deleted_neighbors(grid, x, y, n, m, is_connected)101 cnts = len(deleted_neighbors)-1102 out[i] = cnts103 ...

Full Screen

Full Screen

1202. Smallest String With Swaps.py

Source:1202. Smallest String With Swaps.py Github

copy

Full Screen

1import collections2#思路: 同一深林的排序3class Solution(object):4 def smallestStringWithSwaps(self, s, pairs):5 """6 :type s: str7 :type pairs: List[List[int]]8 :rtype: str9 """10 graph = collections.defaultdict(dict)11 for ele in pairs:12 graph[ele[0]][ele[1]] = True13 graph[ele[1]][ele[0]] = True14 self.graph = graph15 self.used = set()16 res = []17 s = [ele for ele in s]18 self.s = s19 for index,ele in enumerate(s):20 min_ele = self.dfs(index, set())21 s[index],s[min_ele] = s[min_ele], s[index]22 self.used.add(index)23 res.append(s[min_ele])24 return res25 def dfs(self, index, visited):26 for ele in self.graph[index]:27 if self.if_visited(ele) and ele not in visited:28 visited.add(ele)29 if ord(self.s[index]) < ord(self.s[ele]):30 self.dfs(index, visited)31 else:32 index = ele33 self.dfs(ele, visited)34 return index35 def if_visited(self, ele):36 if ele in self.used:37 return False38 return True39import collections40class Solution:41 def dfs(self, i, is_connected):42 for neighbour in self.graph[i]:43 if neighbour not in self.visited:44 # is_connected.append(self.s[neighbour])45 is_connected.append(neighbour)46 self.visited.add(neighbour)47 self.dfs(neighbour, is_connected)48 def smallestStringWithSwaps(self, s, pairs):49 # 建图50 graph = collections.defaultdict(list)51 for ele in pairs:52 graph[ele[0]].append(ele[1])53 graph[ele[1]].append(ele[0])54 self.graph = graph55 self.visited = set()56 self.s = s57 # print(self.graph)58 res = list(s)59 for i in range(len(s)):60 if i not in self.visited:61 is_connected = []62 self.dfs(i, is_connected)63 neighbour = [s[i] for i in is_connected]64 neighbour.sort()65 is_connected.sort()66 for j,ele in zip(is_connected, neighbour):67 res[j] = ele68 return "".join(res)69class Solution(object):70 def bfs(self, i, is_connected):71 if i not in self.visited:72 self.visited.add(i)73 is_connected.append(i)74 queue = collections.deque(self.graph[i])75 while queue:76 node = queue.popleft()77 if node not in self.visited:78 self.visited.add(node)79 is_connected.append(node)80 queue.extend(self.graph[node])81 def smallestStringWithSwaps(self, s, pairs):82 """83 :type s: str84 :type pairs: List[List[int]]85 :rtype: str86 """87 graph = collections.defaultdict(list)88 for ele in pairs:89 graph[ele[0]].append(ele[1])90 graph[ele[1]].append(ele[0])91 self.graph = graph92 self.visited = set()93 self.s = s94 res = list(s)95 for i in range(len(s)):96 if i not in self.visited:97 is_connected = []98 self.bfs(i, is_connected)99 neighbour = [s[i] for i in is_connected]100 neighbour.sort()101 is_connected.sort()102 for j, ele in zip(is_connected, neighbour):103 res[j] = ele104 return "".join(res)105if __name__ == '__main__':106 # s = "dcab"107 # pairs = [[0,3],[1,2]]108 s = "dcab"109 pairs = [[0, 3], [1, 2], [0, 2]]...

Full Screen

Full Screen

directed_graph_unweighted_test.py

Source:directed_graph_unweighted_test.py Github

copy

Full Screen

...19 graph.connect("a", "c")20 graph.connect("b", "d")21 graph.connect("c", "d")22 graph.connect("d", "e")23 self.assertTrue(graph.is_connected("a", "b"))24 self.assertTrue(graph.is_connected("a", "c"))25 self.assertTrue(graph.is_connected("b", "d"))26 self.assertTrue(graph.is_connected("c", "d"))27 self.assertTrue(graph.is_connected("d", "e"))28 self.assertFalse(graph.is_connected("a", "e"))29 self.assertFalse(graph.is_connected("a", "d"))30 self.assertFalse(graph.is_connected("b", "c"))31 self.assertFalse(graph.is_connected("b", "e"))32 self.assertFalse(graph.is_connected("c", "e"))33 def test_connect_throws_error_if_vertex_not_found(self):34 graph = DirectedGraphUnweighted()35 with self.assertRaises(KeyError):36 graph.connect("a", "b")37 def test_is_connected_throws_error_if_vertex_not_found(self):38 graph = DirectedGraphUnweighted()39 with self.assertRaises(KeyError):40 graph.is_connected("a", "b")41 def test_neighbours(self):42 """43 a---->b44 | |45 v v46 c---->d---->e47 """48 graph = DirectedGraphUnweighted()49 graph.add_vertex("a")50 graph.add_vertex("b")51 graph.add_vertex("c")52 graph.add_vertex("d")53 graph.add_vertex("e")54 graph.connect("a", "b")...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lisa automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful