Best Python code snippet using autotest_python
Trie.py
Source:Trie.py
1class Trie:23 def __init__(self, R: int = 26, code=int, decode=str):4 self.R = R5 self.edges = [[-1]*self.R]6 self.is_end = [0]7 self.parents = [-1]8 self.size = [0]9 self.node_label = [-1]10 self.code=code11 self.decode=decode1213 def add(self, S: str):14 code, decode = self.code, self.decode15 edges, R, parents, is_end, size, node_label = self.edges, self.R, self.parents, self.is_end, self.size, self.node_label16 p = 017 for s in S:18 s = code(s)19 if edges[p][s] == -1:20 edges[p][s] = len(edges)21 edges.append([-1]*R)22 parents.append(p)23 is_end.append(0)24 size.append(0)25 node_label.append(s)26 size[p] += 127 p = edges[p][s]28 is_end[p] += 129 size[p] += 13031 def find(self, S: str, prefix: bool = True):32 code, decode = self.code, self.decode33 edges, R, parents, is_end = self.edges, self.R, self.parents, self.is_end34 p = 035 for s in S:36 s = code(s)37 if edges[p][s] == -1:38 return False39 p = edges[p][s]40 return prefix or is_end[p] > 04142 def bisect_left(self, S: str):43 """ Sãè¾æ¸ã«åå¨ããå ´å㯠rank é¢æ°"""44 code, decode = self.code, self.decode45 edges, R, parents, is_end, size = self.edges, self.R, self.parents, self.is_end, self.size46 p = 047 res = 048 for s in S:49 s = code(s)50 if edges[p][s] == -1:51 for q in edges[p][:s]:52 if q == -1:53 continue54 res += size[q]55 res += is_end[p]56 return res57 for q in edges[p][:s]:58 if q == -1:59 continue60 res += size[q]61 res += is_end[p]62 p = edges[p][s]6364 return res6566 def bisect_right(self, S: str):67 code, decode = self.code, self.decode68 edges, R, parents, is_end, size = self.edges, self.R, self.parents, self.is_end, self.size69 p = 070 res = 071 for s in S:72 s = code(s)73 if edges[p][s] == -1:74 for q in edges[p][:s]:75 if q == -1:76 continue77 res += size[q]78 res += is_end[p]79 return res80 for q in edges[p][:s]:81 if q == -1:82 continue83 res += size[q]84 res += is_end[p]85 p = edges[p][s]8687 return res+18889 def delete(self, S: str):90 code, decode = self.code, self.decode91 edges, R, parents, is_end, size, parents, node_label = self.edges, self.R, self.parents, self.is_end, self.size, self.parents, self.node_label92 p = 093 for s in S:94 s = code(s)95 if edges[p][s] == -1:96 return False97 p = edges[p][s]98 if is_end[p] > 0:99 is_end[p] -= 1100 while p != -1:101 size[p] -= 1102 if size[p] == 0:103 edges[parents[p]][node_label[p]] = -1104 p = parents[p]105106 def minimum(self, k: int = 0):107 code, decode = self.code, self.decode108 edges, size, is_end = self.edges, self.size, self.is_end109 p = 0110 res = []111 k += 1112 while k > 0:113 k -= is_end[p]114 if k <= 0:115 return res116 for s, q in enumerate(edges[p]):117 if q == -1:118 continue119 if size[q] >= k:120 p = q121 res.append(decode(s))122 break123 else:124 k -= size[q]125 return res126127 def maximum(self, k: int = 0):128 return self.minimum(len(self)-1-k)129130 def count(self):131 """ ç»é²æ¸ã¿åèªæ° """132 return len(self)133134 def __len__(self):135 return self.size[0]136137 def __iter__(self):138 code, decode = self.code, self.decode139 edges, R, is_end, base, parents, node_label = self.edges, self.R, self.is_end, self.code, self.parents, self.node_label140141 next_set = [(0, [])]142 while next_set:143 p, S = next_set.pop()144 if is_end[p]:145 for _ in range(is_end[p]):146 yield S147 for s, q in list(enumerate(edges[p]))[::-1]:148 if q == -1:149 continue150 T = S+[decode(s)]151 next_set.append((q, T))152153 def __str__(self):154 return "\n".join("".join(s) for s in self)155156 def __getitem__(self, i):157 if i < 0:158 i = len(self)-1+i159 return self.minimum(i)160161 def draw(self):162 import networkx as nx163 import matplotlib.pyplot as plt164 from networkx.drawing.nx_pydot import graphviz_layout165166 G = nx.DiGraph()167 labels = {}168 for p, edge in enumerate(self.edges):169 for s, q in enumerate(edge):170 if q == -1:171 continue172 G.add_node(q)173 labels[q] = self.decode(s)174 # labels[q]=(self.decode(s),self.size[q])175 G.add_edge(p, q)176177 pos = graphviz_layout(G, prog="dot")178 nx.draw(G, pos=pos, with_labels=True, labels=labels, node_size=1000)179 plt.axis("off")180 plt.show()181182N = int(input())183Ss = [[ord(c) - ord('a') for c in input()] for _ in range(N)]184185trie = Trie(code=lambda t: t, decode=lambda t: t)186187ret = 0188Ss.sort(key=lambda s: len(s))189print(Ss)190for S in Ss:191 cnter = [0] * 26192 for c in S:193 cnter[c] += 1194 v = 0195 for c in reversed(S):196 for alpha in range(26):197 if cnter[alpha]:198 word_end = trie.edges[v][alpha]199 if word_end != -1 and trie.is_end[word_end] > 0:200 ret += 1201 cnter[c] -= 1202 nv = trie.edges[v][c]203 print(S,v,nv)204 if nv == -1:205 break206 v = nv207208 trie.add(S[::-1])209
...
B.py
Source:B.py
1from itertools import *2from collections import *3from heapq import *4from bisect import *5import math6import sys7sys.setrecursionlimit(1<<20)8class Trie:9 def __init__(self, R: int = 26, code=int, decode=str):10 self.R = R11 self.edges = [[-1]*self.R]12 self.is_end = [0]13 self.parents = [-1]14 self.size = [0]15 self.node_label = [-1]16 self.code=code17 self.decode=decode18 def add(self, S: str):19 code, decode = self.code, self.decode20 edges, R, parents, is_end, size, node_label = self.edges, self.R, self.parents, self.is_end, self.size, self.node_label21 p = 022 for s in S:23 s = code(s)24 if edges[p][s] == -1:25 edges[p][s] = len(edges)26 edges.append([-1]*R)27 parents.append(p)28 is_end.append(0)29 size.append(0)30 node_label.append(s)31 size[p] += 132 p = edges[p][s]33 is_end[p] += 134 size[p] += 135 def find(self, S: str, prefix: bool = True):36 code, decode = self.code, self.decode37 edges, R, parents, is_end = self.edges, self.R, self.parents, self.is_end38 p = 039 for s in S:40 s = code(s)41 if edges[p][s] == -1:42 return False43 p = edges[p][s]44 return prefix or is_end[p] > 045 def bisect_left(self, S: str):46 """ Sãè¾æ¸ã«åå¨ããå ´å㯠rank é¢æ°"""47 code, decode = self.code, self.decode48 edges, R, parents, is_end, size = self.edges, self.R, self.parents, self.is_end, self.size49 p = 050 res = 051 for s in S:52 s = code(s)53 if edges[p][s] == -1:54 for q in edges[p][:s]:55 if q == -1:56 continue57 res += size[q]58 res += is_end[p]59 return res60 for q in edges[p][:s]:61 if q == -1:62 continue63 res += size[q]64 res += is_end[p]65 p = edges[p][s]66 return res67 def bisect_right(self, S: str):68 code, decode = self.code, self.decode69 edges, R, parents, is_end, size = self.edges, self.R, self.parents, self.is_end, self.size70 p = 071 res = 072 for s in S:73 s = code(s)74 if edges[p][s] == -1:75 for q in edges[p][:s]:76 if q == -1:77 continue78 res += size[q]79 res += is_end[p]80 return res81 for q in edges[p][:s]:82 if q == -1:83 continue84 res += size[q]85 res += is_end[p]86 p = edges[p][s]87 return res+188 def delete(self, S: str):89 code, decode = self.code, self.decode90 edges, R, parents, is_end, size, parents, node_label = self.edges, self.R, self.parents, self.is_end, self.size, self.parents, self.node_label91 p = 092 for s in S:93 s = code(s)94 if edges[p][s] == -1:95 return False96 p = edges[p][s]97 if is_end[p] > 0:98 is_end[p] -= 199 while p != -1:100 size[p] -= 1101 if size[p] == 0:102 edges[parents[p]][node_label[p]] = -1103 p = parents[p]104 def minimum(self, k: int = 0):105 code, decode = self.code, self.decode106 edges, size, is_end = self.edges, self.size, self.is_end107 p = 0108 res = []109 k += 1110 while k > 0:111 k -= is_end[p]112 if k <= 0:113 return res114 for s, q in enumerate(edges[p]):115 if q == -1:116 continue117 if size[q] >= k:118 p = q119 res.append(decode(s))120 break121 else:122 k -= size[q]123 return res124 def maximum(self, k: int = 0):125 return self.minimum(len(self)-1-k)126 def count(self):127 """ ç»é²æ¸ã¿åèªæ° """128 return len(self)129 def __len__(self):130 return self.size[0]131 def __iter__(self):132 code, decode = self.code, self.decode133 edges, R, is_end, base, parents, node_label = self.edges, self.R, self.is_end, self.code, self.parents, self.node_label134 next_set = [(0, [])]135 while next_set:136 p, S = next_set.pop()137 if is_end[p]:138 for _ in range(is_end[p]):139 yield S140 for s, q in list(enumerate(edges[p]))[::-1]:141 if q == -1:142 continue143 T = S+[decode(s)]144 next_set.append((q, T))145 def __str__(self):146 return "\n".join("".join(s) for s in self)147 def __getitem__(self, i):148 if i < 0:149 i = len(self)-1+i150 return self.minimum(i)151 def draw(self):152 import networkx as nx153 import matplotlib.pyplot as plt154 from networkx.drawing.nx_pydot import graphviz_layout155 G = nx.DiGraph()156 labels = {}157 for p, edge in enumerate(self.edges):158 for s, q in enumerate(edge):159 if q == -1:160 continue161 G.add_node(q)162 labels[q] = self.decode(s)163 # labels[q]=(self.decode(s),self.size[q])164 G.add_edge(p, q)165 pos = graphviz_layout(G, prog="dot")166 nx.draw(G, pos=pos, with_labels=True, labels=labels, node_size=1000)167 plt.axis("off")168 plt.show()169N = int(input())170Ss = [[ord(c) - ord('a') for c in input()] for _ in range(N)]171trie = Trie(code=lambda t: t, decode=lambda t: t)172ret = 0173Ss.sort(key=lambda s: len(s))174for S in Ss:175 cnter = [0] * 26176 for c in S:177 cnter[c] += 1178 v = 0179 for c in reversed(S):180 for alpha in range(26):181 if cnter[alpha]:182 word_end = trie.edges[v][alpha]183 if word_end != -1 and trie.is_end[word_end] > 0:184 ret += 1185 cnter[c] -= 1186 nv = trie.edges[v][c]187 if nv == -1:188 break189 v = nv190 trie.add(S[::-1])...
C - Belt Conveyor.py
Source:C - Belt Conveyor.py
1import sys2f = open('C - Belt Conveyor.txt', 'r')3sys.stdin = f4h, w = map(int, input().split(' '))5maze = [list(input()) for i in range(h)]6tmp = [0, 0]7rut = [[False] * w for i in range(h)]8rut[0][0] = True9def move(v, tmp):10 is_end = False11 if v == 'U':12 if tmp[0] - 1 < 0:13 is_end = True14 return [tmp, is_end]15 else:16 return [[tmp[0]-1, tmp[1]], is_end]17 if v == 'D':18 if tmp[0] + 1 >= h:19 is_end = True20 return [tmp, is_end]21 else:22 return [[tmp[0]+1, tmp[1]], is_end]23 if v == 'L':24 if tmp[1] - 1 < 0:25 is_end = True26 return [tmp, is_end]27 else:28 return [[tmp[0], tmp[1] - 1], is_end]29 if v == 'R':30 if tmp[1] + 1 >= w:31 is_end = True32 return [tmp, is_end]33 else:34 return [[tmp[0], tmp[1] + 1], is_end]35is_end = False36while not is_end:37 tmp, is_end = move(maze[tmp[0]][tmp[1]], tmp)38 if not is_end and rut[tmp[0]][tmp[1]]:39 print(-1)40 exit()41 rut[tmp[0]][tmp[1]] = True...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!