How to use get_parent method in Slash

Best Python code snippet using slash

Basics.py

Source:Basics.py Github

copy

Full Screen

1from src.engine.types import Helpers2import random3import traceback4import os5import pandas as pd6class Basics:7 parent = ""8 subsector = ""9 data = []10 def __init__(self, parent, subsector, data):11 self.parent = parent12 self.subsector = subsector13 self.data = data14 def final(self):15 self.mc()16 self.div()17 self.beta()18 self.price()19 self.pt()20 self.up()21 self.tpe()22 self.fpe()23 self.ps()24 self.pb()25 self.pf()26 self.pMargin()27 self.opMargin()28 self.retOnAsset()29 self.retOnEquity()30 self.evToRev()31 self.evToEbitda()32 return str(self.data).replace("'", "\"")33 def mc(self):34 count = 035 parent_t = ""36 for tick in self.data:37 if self.parent == "Customs":38 if os.path.isfile('./data/tickers/Categories.csv'):39 get_parent = pd.read_csv('./data/tickers/Categories.csv')40 isSubsector = get_parent['Subsector'] == tick['Category']41 parentSect = get_parent[isSubsector]42 parent_t = str(parentSect['ParentSector'].head(1).item())43 else:44 parent_t = self.parent45 try:46 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")47 if "ERROR" in p_e:48 self.data[count]['basics'] = {'mc': '-999'}49 else:50 if p_e[str(list(p_e.keys())[0])]['Market Cap'] == 'N/A':51 self.data[count]['basics'] = {'mc': '-999'}52 else:53 m_cap = str(p_e[str(list(p_e.keys())[0])]['Market Cap'])54 if "B" in m_cap:55 m_cap = float(m_cap.replace("B", ""))56 elif "M" in m_cap:57 m_cap = round(float(m_cap.replace("M", ""))/1000, 2)58 elif "T" in m_cap:59 m_cap = round(float(m_cap.replace("T", ""))*1000, 2)60 else:61 m_cap = round(float(m_cap.replace(",", ""))/1000000, 2)62 self.data[count]['basics'] = {'mc': str(m_cap)}63 except:64 self.data[count]['basics'] = {'mc': '-999'}65 pass66 count = count + 167 def div(self):68 count = 069 parent_t = ""70 for tick in self.data:71 if self.parent == "Customs":72 if os.path.isfile('./data/tickers/Categories.csv'):73 get_parent = pd.read_csv('./data/tickers/Categories.csv')74 isSubsector = get_parent['Subsector'] == tick['Category']75 parentSect = get_parent[isSubsector]76 parent_t = str(parentSect['ParentSector'].head(1).item())77 else:78 parent_t = self.parent79 try:80 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")81 if "ERROR" in p_e:82 self.data[count]['basics']['div'] = '-999'83 else:84 if 'N/A' in p_e[str(list(p_e.keys())[0])]['Forward Dividend & Yield']:85 self.data[count]['basics']['div'] = '-999'86 else:87 div = str(p_e[str(list(p_e.keys())[0])]['Forward Dividend & Yield'])88 div = div.split("(")[1]89 div = div.split("%")[0]90 self.data[count]['basics']['div'] = str(div)91 except:92 self.data[count]['basics']['div'] = '-999'93 pass94 count = count + 195 def beta(self):96 count = 097 parent_t = ""98 for tick in self.data:99 if self.parent == "Customs":100 if os.path.isfile('./data/tickers/Categories.csv'):101 get_parent = pd.read_csv('./data/tickers/Categories.csv')102 isSubsector = get_parent['Subsector'] == tick['Category']103 parentSect = get_parent[isSubsector]104 parent_t = str(parentSect['ParentSector'].head(1).item())105 else:106 parent_t = self.parent107 try:108 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")109 if "ERROR" in p_e:110 self.data[count]['basics']['beta'] = '-999'111 else:112 if 'N/A' in p_e[str(list(p_e.keys())[0])]['Beta (5Y Monthly)']:113 self.data[count]['basics']['beta'] = '-999'114 else:115 beta = str(p_e[str(list(p_e.keys())[0])]['Beta (5Y Monthly)'])116 self.data[count]['basics']['beta'] = str(beta)117 except:118 self.data[count]['basics']['beta'] = '-999'119 pass120 count = count + 1121 def price(self):122 count = 0123 parent_t = ""124 for tick in self.data:125 if self.parent == "Customs":126 if os.path.isfile('./data/tickers/Categories.csv'):127 get_parent = pd.read_csv('./data/tickers/Categories.csv')128 isSubsector = get_parent['Subsector'] == tick['Category']129 parentSect = get_parent[isSubsector]130 parent_t = str(parentSect['ParentSector'].head(1).item())131 else:132 parent_t = self.parent133 try:134 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")135 if "ERROR" in p_e:136 self.data[count]['basics']['price'] = '-999'137 else:138 if 'N/A' in p_e[str(list(p_e.keys())[0])]['value']:139 self.data[count]['basics']['price'] = '-999'140 else:141 price = str(p_e[str(list(p_e.keys())[0])]['value'])142 self.data[count]['basics']['price'] = str(price)143 except:144 self.data[count]['basics']['price'] = '-999'145 pass146 count = count + 1147 def pt(self):148 count = 0149 parent_t = ""150 for tick in self.data:151 if self.parent == "Customs":152 if os.path.isfile('./data/tickers/Categories.csv'):153 get_parent = pd.read_csv('./data/tickers/Categories.csv')154 isSubsector = get_parent['Subsector'] == tick['Category']155 parentSect = get_parent[isSubsector]156 parent_t = str(parentSect['ParentSector'].head(1).item())157 else:158 parent_t = self.parent159 try:160 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")161 if "ERROR" in p_e:162 self.data[count]['basics']['target'] = '-999'163 else:164 if 'N/A' in p_e[str(list(p_e.keys())[0])]['1y Target Est']:165 self.data[count]['basics']['target'] = '-999'166 else:167 target = str(p_e[str(list(p_e.keys())[0])]['1y Target Est'])168 self.data[count]['basics']['target'] = str(target)169 except:170 self.data[count]['basics']['target'] = '-999'171 pass172 count = count + 1173 def up(self):174 count = 0175 parent_t = ""176 for tick in self.data:177 if self.parent == "Customs":178 if os.path.isfile('./data/tickers/Categories.csv'):179 get_parent = pd.read_csv('./data/tickers/Categories.csv')180 isSubsector = get_parent['Subsector'] == tick['Category']181 parentSect = get_parent[isSubsector]182 parent_t = str(parentSect['ParentSector'].head(1).item())183 else:184 parent_t = self.parent185 try:186 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")187 if "ERROR" in p_e:188 self.data[count]['basics']['upside'] = '-999'189 else:190 if 'N/A' in p_e[str(list(p_e.keys())[0])]['1y Target Est']:191 self.data[count]['basics']['upside'] = '-999'192 else:193 target = float(p_e[str(list(p_e.keys())[0])]['1y Target Est'].replace(",", ""))194 price = float(p_e[str(list(p_e.keys())[0])]['value'].replace(",", ""))195 upside = round(((target - price)/price)*100, 2)196 if upside>300:197 raise Exception("Upaside over 300% - Invalid")198 else:199 self.data[count]['basics']['upside'] = str(upside)200 except:201 self.data[count]['basics']['upside'] = '-999'202 pass203 count = count + 1204 def pMargin(self):205 count = 0206 parent_t = ""207 for tick in self.data:208 if self.parent == "Customs":209 if os.path.isfile('./data/tickers/Categories.csv'):210 get_parent = pd.read_csv('./data/tickers/Categories.csv')211 isSubsector = get_parent['Subsector'] == tick['Category']212 parentSect = get_parent[isSubsector]213 parent_t = str(parentSect['ParentSector'].head(1).item())214 else:215 parent_t = self.parent216 try:217 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")218 if "ERROR" in p_e:219 self.data[count]['basics']['pMar'] = '-999'220 else:221 if 'N/A' in p_e[str(list(p_e.keys())[0])]['Profit Margin ']:222 self.data[count]['basics']['pMar'] = '-999'223 else:224 target = float(p_e[str(list(p_e.keys())[0])]['Profit Margin '].replace(",", "").replace("%", ""))225 if target>100:226 raise Exception("Profit margin over 100% - Invalid")227 else:228 self.data[count]['basics']['pMar'] = str(target)229 except:230 self.data[count]['basics']['pMar'] = '-999'231 pass232 count = count + 1233 def opMargin(self):234 count = 0235 parent_t = ""236 for tick in self.data:237 if self.parent == "Customs":238 if os.path.isfile('./data/tickers/Categories.csv'):239 get_parent = pd.read_csv('./data/tickers/Categories.csv')240 isSubsector = get_parent['Subsector'] == tick['Category']241 parentSect = get_parent[isSubsector]242 parent_t = str(parentSect['ParentSector'].head(1).item())243 else:244 parent_t = self.parent245 try:246 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")247 if "ERROR" in p_e:248 self.data[count]['basics']['opMar'] = '-999'249 else:250 if 'N/A' in p_e[str(list(p_e.keys())[0])]['Operating Margin (ttm)']:251 self.data[count]['basics']['opMar'] = '-999'252 else:253 target = float(254 p_e[str(list(p_e.keys())[0])]['Operating Margin (ttm)'].replace(",", "").replace("%", ""))255 if target > 100:256 raise Exception("Operating margin over 100% - Invalid")257 else:258 self.data[count]['basics']['opMar'] = str(target)259 except:260 self.data[count]['basics']['opMar'] = '-999'261 pass262 count = count + 1263 def retOnAsset(self):264 count = 0265 parent_t = ""266 for tick in self.data:267 if self.parent == "Customs":268 if os.path.isfile('./data/tickers/Categories.csv'):269 get_parent = pd.read_csv('./data/tickers/Categories.csv')270 isSubsector = get_parent['Subsector'] == tick['Category']271 parentSect = get_parent[isSubsector]272 parent_t = str(parentSect['ParentSector'].head(1).item())273 else:274 parent_t = self.parent275 try:276 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")277 if "ERROR" in p_e:278 self.data[count]['basics']['retAs'] = '-999'279 else:280 if 'N/A' in p_e[str(list(p_e.keys())[0])]['Return on Assets (ttm)']:281 self.data[count]['basics']['retAs'] = '-999'282 else:283 target = float(284 p_e[str(list(p_e.keys())[0])]['Return on Assets (ttm)'].replace(",", "").replace("%", ""))285 if target > 100:286 raise Exception("Return on asset over 100% - Invalid")287 else:288 self.data[count]['basics']['retAs'] = str(target)289 except:290 self.data[count]['basics']['retAs'] = '-999'291 pass292 count = count + 1293 def retOnEquity(self):294 count = 0295 parent_t = ""296 for tick in self.data:297 if self.parent == "Customs":298 if os.path.isfile('./data/tickers/Categories.csv'):299 get_parent = pd.read_csv('./data/tickers/Categories.csv')300 isSubsector = get_parent['Subsector'] == tick['Category']301 parentSect = get_parent[isSubsector]302 parent_t = str(parentSect['ParentSector'].head(1).item())303 else:304 parent_t = self.parent305 try:306 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")307 if "ERROR" in p_e:308 self.data[count]['basics']['retEq'] = '-999'309 else:310 if 'N/A' in p_e[str(list(p_e.keys())[0])]['Return on Equity (ttm)']:311 self.data[count]['basics']['retEq'] = '-999'312 else:313 target = float(314 p_e[str(list(p_e.keys())[0])]['Return on Equity (ttm)'].replace(",", "").replace("%", ""))315 if target > 100:316 raise Exception("Return on asset over 100% - Invalid")317 else:318 self.data[count]['basics']['retEq'] = str(target)319 except:320 self.data[count]['basics']['retEq'] = '-999'321 pass322 count = count + 1323 def fpe(self):324 count = 0325 parent_t = ""326 for tick in self.data:327 if self.parent == "Customs":328 if os.path.isfile('./data/tickers/Categories.csv'):329 get_parent = pd.read_csv('./data/tickers/Categories.csv')330 isSubsector = get_parent['Subsector'] == tick['Category']331 parentSect = get_parent[isSubsector]332 parent_t = str(parentSect['ParentSector'].head(1).item())333 else:334 parent_t = self.parent335 try:336 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "ks")337 if "ERROR" in p_e:338 self.data[count]['basics']['fpe'] = '-999'339 else:340 if p_e['Current']['Forward P/E 1'] == 'N/A':341 self.data[count]['basics']['fpe'] = '-999'342 else:343 self.data[count]['basics']['fpe'] = p_e['Current']['Forward P/E 1']344 except:345 self.data[count]['basics']['fpe'] = '-999'346 pass347 count = count + 1348 def tpe(self):349 count = 0350 parent_t = ""351 for tick in self.data:352 if self.parent == "Customs":353 if os.path.isfile('./data/tickers/Categories.csv'):354 get_parent = pd.read_csv('./data/tickers/Categories.csv')355 isSubsector = get_parent['Subsector'] == tick['Category']356 parentSect = get_parent[isSubsector]357 parent_t = str(parentSect['ParentSector'].head(1).item())358 else:359 parent_t = self.parent360 try:361 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "ks")362 if "ERROR" in p_e:363 self.data[count]['basics']['tpe'] = '-999'364 else:365 if p_e['Current']['Trailing P/E '] == 'N/A':366 self.data[count]['basics']['tpe'] = '-999'367 else:368 self.data[count]['basics']['tpe'] = p_e['Current']['Trailing P/E ']369 except:370 self.data[count]['basics']['tpe'] = '-999'371 pass372 count = count + 1373 def ps(self):374 count = 0375 parent_t = ""376 for tick in self.data:377 if self.parent == "Customs":378 if os.path.isfile('./data/tickers/Categories.csv'):379 get_parent = pd.read_csv('./data/tickers/Categories.csv')380 isSubsector = get_parent['Subsector'] == tick['Category']381 parentSect = get_parent[isSubsector]382 parent_t = str(parentSect['ParentSector'].head(1).item())383 else:384 parent_t = self.parent385 try:386 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "ks")387 if "ERROR" in p_e:388 self.data[count]['basics']['ps'] = '-999'389 else:390 if p_e['Current']['Price/Sales (ttm)'] == 'N/A':391 self.data[count]['basics']['ps'] = '-999'392 else:393 self.data[count]['basics']['ps'] = p_e['Current']['Price/Sales (ttm)']394 except:395 self.data[count]['basics']['ps'] = '-999'396 pass397 count = count + 1398 def pb(self):399 count = 0400 parent_t = ""401 for tick in self.data:402 if self.parent == "Customs":403 if os.path.isfile('./data/tickers/Categories.csv'):404 get_parent = pd.read_csv('./data/tickers/Categories.csv')405 isSubsector = get_parent['Subsector'] == tick['Category']406 parentSect = get_parent[isSubsector]407 parent_t = str(parentSect['ParentSector'].head(1).item())408 else:409 parent_t = self.parent410 try:411 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "ks")412 if "ERROR" in p_e:413 self.data[count]['basics']['pb'] = '-999'414 else:415 if p_e['Current']['Price/Book (mrq)'] == 'N/A':416 self.data[count]['basics']['pb'] = '-999'417 else:418 self.data[count]['basics']['pb'] = p_e['Current']['Price/Book (mrq)']419 except:420 self.data[count]['basics']['pb'] = '-999'421 pass422 count = count + 1423 def pf(self):424 count = 0425 parent_t = ""426 for tick in self.data:427 if self.parent == "Customs":428 if os.path.isfile('./data/tickers/Categories.csv'):429 get_parent = pd.read_csv('./data/tickers/Categories.csv')430 isSubsector = get_parent['Subsector'] == tick['Category']431 parentSect = get_parent[isSubsector]432 parent_t = str(parentSect['ParentSector'].head(1).item())433 else:434 parent_t = self.parent435 try:436 quote = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "quote")437 m_cap = str(quote[str(list(quote.keys())[0])]['Market Cap'])438 if "T" in m_cap:439 m_cap = int(float(m_cap.replace("T", "")) * 1000000000000)440 elif "B" in m_cap:441 m_cap = int(float(m_cap.replace("B", "")) * 1000000000)442 elif "M" in m_cap:443 m_cap = int(float(m_cap.replace("M", "")) * 1000000)444 cf = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "cf")['TTM']445 fin = int(str(cf['Free Cash Flow']).replace(",", "")) * 1000446 self.data[count]['basics']['pf'] = str(round(float(m_cap / fin), 2))447 except:448 self.data[count]['basics']['pf'] = str("-999")449 pass450 count = count + 1451 def evToRev(self):452 count = 0453 parent_t = ""454 for tick in self.data:455 if self.parent == "Customs":456 if os.path.isfile('./data/tickers/Categories.csv'):457 get_parent = pd.read_csv('./data/tickers/Categories.csv')458 isSubsector = get_parent['Subsector'] == tick['Category']459 parentSect = get_parent[isSubsector]460 parent_t = str(parentSect['ParentSector'].head(1).item())461 else:462 parent_t = self.parent463 try:464 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "ks")465 if "ERROR" in p_e:466 self.data[count]['basics']['evRev'] = '-999'467 else:468 if p_e['Current']['Enterprise Value/Revenue 3'] == 'N/A':469 self.data[count]['basics']['evRev'] = '-999'470 else:471 self.data[count]['basics']['evRev'] = p_e['Current']['Enterprise Value/Revenue 3']472 except:473 self.data[count]['basics']['evRev'] = '-999'474 pass475 count = count + 1476 def evToEbitda(self):477 count = 0478 parent_t = ""479 for tick in self.data:480 if self.parent == "Customs":481 if os.path.isfile('./data/tickers/Categories.csv'):482 get_parent = pd.read_csv('./data/tickers/Categories.csv')483 isSubsector = get_parent['Subsector'] == tick['Category']484 parentSect = get_parent[isSubsector]485 parent_t = str(parentSect['ParentSector'].head(1).item())486 else:487 parent_t = self.parent488 try:489 p_e = Helpers.get_by_type(parent_t, tick['Category'], tick['Ticker'], "ks")490 if "ERROR" in p_e:491 self.data[count]['basics']['evEbit'] = '-999'492 else:493 if p_e['Current']['Enterprise Value/EBITDA 7'] == 'N/A':494 self.data[count]['basics']['evEbit'] = '-999'495 else:496 self.data[count]['basics']['evEbit'] = p_e['Current']['Enterprise Value/EBITDA 7']497 except:498 self.data[count]['basics']['evEbit'] = '-999'499 pass...

Full Screen

Full Screen

distances_with_trie.py

Source:distances_with_trie.py Github

copy

Full Screen

...3TEST_MODULE_FLAG = True4# Si al terminar una etapa el menor valor es >threshold se podría parar5# - El nº filas antes era uno más la longitud de una cadena, ahora es el nº estados del Trie.6# - Cuando miras una dependencia vertical en el grafo de dependencia antes venía de la letra anterior7# [i-1,j], ahora con [trie.get_parent(i),j].8# - Cuando miras una dependencia diagonal en el grafo de dependencia era una sustitución que venía de9# [i-1,j-1] y ahora será de [trie.get_parent(i),j-1]. Para determinar si es sustitución o acierto hay que10# consultar trie.get_label(i).11def dp_levenshtein_backwards_threshold_trie(term_trie, ref, threshold):12 """13 term_trie: Trie of term word14 ref: String15 Calcula la distancia de Levenshtein entre las cadenas term y ref16 con un umbral maximo threshold17 """18 # Res: matriz estructura term_trie.get_num_states x len(ref) + 119 res = init_matriz_trie(term_trie, ref)20 for i in range(1, term_trie.get_num_states()):21 # Si la distancia de el padre es mayor al threshold evitamos j iteraciones22 if min(res[term_trie.get_parent(i), :]) > threshold:23 continue24 for j in range(1, len(ref) + 1):25 res[i,j] = min(26 res[term_trie.get_parent(i),j-1] if term_trie.get_label(i) == ref[j-1] else 1 + res[term_trie.get_parent(i),j-1],27 1 + res[term_trie.get_parent(i),j],28 1 + res[i, j-1]29 )30 result = [(i, res[i, len(ref)]) for i in range(0, term_trie.get_num_states()) if term_trie.is_final(i) and res[i, len(ref)] <= threshold]31 return result32def dp_restricted_damerau_backwards_threshold_trie(term_trie, ref, threshold):33 """34 Calcula la distancia de Damerau Levenshtein Restringida entre las cadenas ref y term.35 Únicamente añade la función de que si dos carácteres seguidos aparecen al reves estos supone coste 1 en vez de coste 2.36 """37 # Simula el infinito38 INF = term_trie.get_num_states()+ len(ref)39 res = init_matriz_trie(term_trie, ref)40 for i in range(1, term_trie.get_num_states()):41 for j in range(1, len(ref) + 1):42 if j == 1 or term_trie.get_parent(i) == term_trie.get_root():43 res[i,j] = min(44 res[term_trie.get_parent(i),j-1] if term_trie.get_label(i) == ref[j-1] else 1 + res[term_trie.get_parent(i),j-1],45 1 + res[term_trie.get_parent(i),j],46 1 + res[i, j-1]47 )48 else:49 res[i,j] = min(50 res[term_trie.get_parent(i),j-1] if term_trie.get_label(i) == ref[j-1] else 1 + res[term_trie.get_parent(i),j-1],51 1 + res[term_trie.get_parent(i),j],52 1 + res[i, j-1],53 1 + res[term_trie.get_parent(term_trie.get_parent(i)),j-2] if term_trie.get_label(term_trie.get_parent(i)) == ref[j-1] and term_trie.get_label(i) == ref[j-2] else INF54 )55 result = [(i, res[i, len(ref)]) for i in range(0, term_trie.get_num_states()) if term_trie.is_final(i) and res[i, len(ref)] <= threshold]56 return result57def dp_intermediate_damerau_backwards_threshold_trie(term_trie, ref, threshold):58 """59 Calcula la distancia de Damerau Levenshtein Restringida entre las cadenas ref y term.60 Únicamente añade la función de que si dos carácteres seguidos aparecen al reves estos supone coste 1 en vez de coste 2.61 """62 # Simula el infinito63 INF = term_trie.get_num_states()+ len(ref)64 res = init_matriz_trie(term_trie, ref)65 for i in range(0, term_trie.get_num_states()):66 for j in range(1, len(ref) + 1):67 init_actual = min(68 res[term_trie.get_parent(i),j-1] if term_trie.get_label(i) == ref[j-1] else 1 + res[term_trie.get_parent(i),j-1],69 1 + res[term_trie.get_parent(i),j],70 1 + res[i, j-1]71 )72 if j>1 and term_trie.get_parent(i) != term_trie.get_root() and term_trie.get_label(term_trie.get_parent(i)) == ref[j-1] and term_trie.get_label(i) == ref[j-2]:73 res[i,j] = min(74 init_actual,75 1 + res[term_trie.get_parent(term_trie.get_parent(i)),j-2]76 )77 elif j > 2 and term_trie.get_parent(i) != term_trie.get_root() and term_trie.get_label(term_trie.get_parent(i)) == ref[j-1] and term_trie.get_label(i) == ref[j-3]:78 res [i,j] = min(79 init_actual,80 2 + res[term_trie.get_parent(term_trie.get_parent(i)), j-3]81 )82 elif term_trie.get_parent(i) != term_trie.get_root() and term_trie.get_parent(term_trie.get_parent(i)) != term_trie.get_root() and j > 1 and term_trie.get_label(term_trie.get_parent(term_trie.get_parent(i))) == ref[j-1] and term_trie.get_label(i) == ref[j-2]:83 res [i,j] = min(84 init_actual,85 2 + res[term_trie.get_parent(term_trie.get_parent(term_trie.get_parent(i))), j-2]86 )87 else:88 res[i,j] = init_actual89 result = [(i, res[i, len(ref)]) for i in range(0, term_trie.get_num_states()) if term_trie.is_final(i) and res[i, len(ref)] <= threshold]...

Full Screen

Full Screen

백준 1717.py

Source:백준 1717.py Github

copy

Full Screen

1import sys2sys.setrecursionlimit(10**6)3n, m = map(int, sys.stdin.readline().split())4arr = [i for i in range(n+1)]5def get_parent(n):6 if arr[n] != n:7 arr[n] = get_parent(arr[n])8 return arr[n]9def union_(u, v):10 u = get_parent(u)11 v = get_parent(v)12 if u > v:13 arr[v] = u14 else:15 arr[u] = v16# 또 다른 맞은 함수17def union_(u, v):18 u = get_parent(u)19 v = get_parent(v)20 if u != v:21 arr[v]=u22 else:23 return arr[u]24# 또 다른 함수 (그냥 쓰면 틀리는 이유: 재귀가 너무 많이 돌면 터짐)25# sys.setrecursionlimit(10**6)를 추가해주면 맞음.26def union_(u, v):27 u = get_parent(u)28 v = get_parent(v)29 if u < v:30 arr[v] = u31 else:32 arr[u] = v33def find_parent(u, v):34 u = get_parent(u)35 v = get_parent(v)36 if u == v:37 print("YES")38 else:39 print("NO")40for _ in range(m):41 r, a, b = map(int, sys.stdin.readline().split())42 if not r:43 union_(a, b)44 else:45 a = get_parent(a)46 b = get_parent(b)47 if a==b:48 print("YES")49 else:...

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 Slash 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