Best Python code snippet using autotest_python
ABC253C.py
Source:ABC253C.py
1from bisect import insort, bisect_left, bisect_right2__file = open(0)3readline = __file.readline4class OrderedMultiList:5 class FenwickTree:6 def __init__(self, array):7 n = len(array)8 self.__container = [0] + array[:]9 self.__size = len(self.__container)10 self.__depth = n.bit_length()11 for i in range(n):12 j = i | (i + 1)13 if j < n:14 self.__container[j + 1] += self.__container[i + 1]15 def add(self, i, x):16 i += 117 while i < self.__size:18 self.__container[i] += x19 i += i & (-i)20 def sum(self, r):21 s = 022 while r > 0:23 s += self.__container[r]24 r -= r & (-r)25 return s26 def upper_bound(self, s):27 w, r = 0, 028 for i in reversed(range(self.__depth)):29 k = r + (1 << i)30 if k < self.__size and w + self.__container[k] <= s:31 w += self.__container[k]32 r += 1 << i33 return r34 __load = 100035 def __init__(self):36 self.__lists = []37 self.__maxes = []38 self.__sizes_list = []39 self.__sizes_ft = self.FenwickTree([])40 self.__len = 041 self.__block_count = 042 def add(self, x):43 if self.__len > 0:44 li = bisect_left(self.__maxes, x)45 if li == len(self.__maxes):46 li -= 147 self.__maxes[-1] = x48 _list = self.__lists[li]49 insort(_list, x)50 self.__sizes_list[li] += 151 self.__sizes_ft.add(li, 1)52 if len(_list) >= 2 * self.__load:53 self.__split(li)54 else:55 self.__maxes.append(x)56 self.__lists.append([x])57 self.__sizes_list.append(1)58 self.__build_ft()59 self.__block_count += 160 self.__len += 161 def discard(self, x):62 if self.__len == 0:63 return False64 li = bisect_left(self.__maxes, x)65 if li == self.__block_count:66 return False67 __list = self.__lists[li]68 lli = bisect_left(__list, x)69 y = __list[lli]70 if x != y:71 return False72 del __list[lli]73 if not __list:74 del self.__lists[li]75 del self.__maxes[li]76 del self.__sizes_list[li]77 self.__block_count -= 178 self.__len -= 179 self.__build_ft()80 return True81 if x == self.__maxes[li]:82 self.__maxes[li] = __list[-1]83 self.__len -= 184 self.__sizes_ft.add(li, -1)85 self.__sizes_list[li] -= 186 if self.__block_count >= 2 and 2 * self.__sizes_list[li] <= self.__load:87 self.__marge(li)88 return True89 def contains(self, x):90 if self.__len == 0:91 return False92 li = bisect_left(self.__maxes, x)93 if li == self.__block_count:94 return False95 _list = self.__lists[li]96 lli = bisect_left(_list, x)97 y = _list[lli]98 return x == y99 def index(self, x):100 if self.__len == 0:101 return None102 li = bisect_left(self.__maxes, x)103 if li == self.__block_count:104 return None105 _list = self.__lists[li]106 lli = bisect_left(_list, x)107 y = _list[lli]108 if x != y:109 return None110 ft = self.__sizes_ft111 return ft.sum(li) + lli112 def index_right(self, x):113 if self.__len == 0:114 return None115 li = bisect_right(self.__maxes, x)116 if li == self.__block_count:117 y = self.__lists[-1][-1]118 return self.__pos(li - 1, self.__sizes_list[-1] - 1) if y == x else None119 _list = self.__lists[li]120 lli = bisect_right(_list, x) - 1121 if lli == -1:122 if li == 0:123 return None124 y = self.__lists[li - 1][-1]125 return self.__pos(li - 1, self.__sizes_list[li - 1] - 1) if y == x else None126 y = _list[lli]127 if x != y:128 return None129 return self.__pos(li, lli)130 def bisect_left(self, x):131 li = bisect_left(self.__maxes, x)132 if li == self.__block_count:133 return self.__pos(self.__block_count, 0)134 lli = bisect_left(self.__lists[li], x)135 return self.__pos(li, lli)136 def bisect_right(self, x):137 li = bisect_right(self.__maxes, x)138 if li == self.__block_count:139 return self.__pos(self.__block_count, 0)140 lli = bisect_right(self.__lists[li], x)141 return self.__pos(li, lli)142 def count(self, x):143 return self.bisect_right(x) - self.bisect_left(x)144 def le(self, x):145 if self.__len == 0:146 return None147 li = bisect_left(self.__maxes, x)148 if li == self.__block_count:149 return self.__lists[li - 1][-1]150 _list = self.__lists[li]151 lli = bisect_right(_list, x)152 if lli == 0:153 if li == 0:154 return None155 return self.__lists[li - 1][-1]156 return self.__lists[li][lli - 1]157 def lt(self, x):158 if self.__len == 0:159 return None160 li = bisect_left(self.__maxes, x)161 if li == self.__block_count:162 return self.__lists[li - 1][-1]163 _list = self.__lists[li]164 lli = bisect_left(_list, x)165 if lli == 0:166 if li == 0:167 return None168 return self.__lists[li - 1][-1]169 return self.__lists[li][lli - 1]170 def ge(self, x):171 if self.__len == 0:172 return None173 li = bisect_left(self.__maxes, x)174 if li == self.__block_count:175 y = self.__lists[-1][-1]176 return y if y == x else None177 _list = self.__lists[li]178 lli = bisect_left(_list, x)179 return self.__lists[li][lli]180 def gt(self, x):181 if self.__len == 0:182 return None183 li = bisect_right(self.__maxes, x)184 if li == self.__block_count:185 return None186 _list = self.__lists[li]187 lli = bisect_right(_list, x)188 return self.__lists[li][lli]189 @property190 def max(self):191 return self[self.__len - 1]192 @property193 def min(self):194 return self[0]195 def __build_ft(self):196 self.__sizes_ft = self.FenwickTree(self.__sizes_list)197 def __pos(self, li, lli):198 return self.__sizes_ft.sum(li) + lli199 def __split(self, li):200 _list = self.__lists[li]201 sz = self.__sizes_list[li]202 self.__maxes.insert(li + 1, _list[-1])203 self.__lists.insert(li + 1, _list[self.__load:])204 del _list[self.__load:]205 self.__maxes[li] = _list[-1]206 self.__sizes_list[li] = self.__load207 self.__sizes_list.insert(li + 1, sz - self.__load)208 self.__build_ft()209 self.__block_count += 1210 def __marge(self, li):211 if li == 0:212 self.__lists[0].extend(self.__lists[1])213 self.__sizes_list[0] += self.__sizes_list[1]214 self.__maxes[0] = self.__maxes[1]215 del self.__lists[1]216 del self.__maxes[1]217 del self.__sizes_list[1]218 self.__block_count -= 1219 if self.__sizes_list[0] >= 2 * self.__load:220 return self.__split(0)221 else:222 self.__lists[li - 1].extend(self.__lists[li])223 self.__sizes_list[li - 1] += self.__sizes_list[li]224 self.__maxes[li - 1] = self.__maxes[li]225 del self.__lists[li]226 del self.__maxes[li]227 del self.__sizes_list[li]228 self.__block_count -= 1229 if self.__sizes_list[li - 1] >= 2 * self.__load:230 return self.__split(li - 1)231 self.__build_ft()232 def __len__(self):233 return self.__len234 def __contains__(self, x):235 return self.contains(x)236 def __getitem__(self, i):237 if i < 0:238 i += self.__len239 ft = self.__sizes_ft240 li = ft.upper_bound(i)241 lli = i - ft.sum(li)242 return self.__lists[li][lli]243 def get_all_values(self):244 ret = []245 for _list in self.__lists:246 ret.extend(_list)247 return ret248def main():249 q = int(input())250 s = OrderedMultiList()251 for _ in range(q):252 query = list(map(int,input().split()))253 if query[0]==1:254 x = query[1]255 s.add(x)256 elif query[0]==2:257 x,c = query[1:]258 for _ in range(c):259 f = s.discard(x)260 if not f:261 break262 else:263 print(s.max-s.min)264if __name__ == '__main__':...
aws_topic_history_reporter.py
Source:aws_topic_history_reporter.py
1"""2Created on 17 Jan 20183@author: Bruno Beloff (bruno.beloff@southcoastscience.com)4source repo: scs_analysis5"""6import logging7import time8from scs_core.data.timedelta import Timedelta9from scs_core.sys.logging import Logging10# --------------------------------------------------------------------------------------------------------------------11class BatchDownloadReporter(object):12 """13 classdocs14 """15 # ----------------------------------------------------------------------------------------------------------------16 def __init__(self):17 """18 Constructor19 """20 self.__block_count = 021 self.__document_count = 022 self.__start_time = time.time()23 self.__logger = Logging.getLogger()24 # ----------------------------------------------------------------------------------------------------------------25 def reset(self):26 self.__block_count = 027 self.__document_count = 028 self.__start_time = time.time()29 def print(self, block_length, block_start=None):30 if Logging.level() > logging.INFO:31 return32 self.__block_count += 133 self.__document_count += block_length34 elapsed_time = int(round(time.time() - self.__start_time))35 elapsed_delta = Timedelta(seconds=elapsed_time)36 elapsed = elapsed_delta.as_json()37 if block_start is None:38 self.__logger.info("docs:%d elapsed:%s" % (self.__document_count, elapsed))39 else:40 self.__logger.info("block start:%s docs:%d elapsed:%s" % (block_start, self.__document_count, elapsed))41 # ----------------------------------------------------------------------------------------------------------------42 @property43 def block_count(self):44 return self.__block_count45 # ----------------------------------------------------------------------------------------------------------------46 def __str__(self, *args, **kwargs):47 return "BatchDownloadReporter:{block_count:%d, document_count:%d, start_time:%d}" % \...
duplicate.py
Source:duplicate.py
1import numpy as np2from profiles.blockassignment.abstract import AbsBlockAssignment3class DuplicateAssignment(AbsBlockAssignment):4 """5 Full duplicated block assignment strategy.6 Every r nodes have the same blocks.7 """8 def __init__(self, node_count: int, redundancy: int):9 super().__init__(node_count, redundancy)10 self.__block_count = node_count // redundancy11 self.__block_ids = list(range(self.__block_count))12 self.__node_ids = np.arange(0, node_count, 1)13 self.__block_2_node = np.split(self.__node_ids, self.__block_count)14 self.__node_2_block = [[block_id] for block_id in self.__block_ids for _ in range(redundancy)]15 @property16 def block_2_node(self):17 return self.__block_2_node18 @property19 def node_2_block(self):20 return self.__node_2_block21 @property22 def block_count(self):23 return self.__block_count24 @property25 def blocks(self):...
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!!