Best Python code snippet using stestr_python
sortedset.py
Source:sortedset.py
1"""Sorted set implementation.2"""3from collections import Set, MutableSet, Sequence4from itertools import chain5import operator as op6from .sortedlist import SortedList, recursive_repr, SortedListWithKey7class SortedSet(MutableSet, Sequence):8 """9 A `SortedSet` provides the same methods as a `set`. Additionally, a10 `SortedSet` maintains its items in sorted order, allowing the `SortedSet` to11 be indexed.12 Unlike a `set`, a `SortedSet` requires items be hashable and comparable.13 """14 def __init__(self, iterable=None, key=None, load=1000, _set=None):15 """16 A `SortedSet` provides the same methods as a `set`. Additionally, a17 `SortedSet` maintains its items in sorted order, allowing the18 `SortedSet` to be indexed.19 An optional *iterable* provides an initial series of items to populate20 the `SortedSet`.21 An optional *key* argument defines a callable that, like the `key`22 argument to Python's `sorted` function, extracts a comparison key from23 each set item. If no function is specified, the default compares the24 set items directly.25 An optional *load* specifies the load-factor of the set. The default26 load factor of '1000' works well for sets from tens to tens of millions27 of elements. Good practice is to use a value that is the cube root of28 the set size. With billions of elements, the best load factor depends29 on your usage. It's best to leave the load factor at the default until30 you start benchmarking.31 """32 # pylint: disable=redefined-variable-type33 self._key = key34 self._load = load35 self._set = set() if _set is None else _set36 _set = self._set37 self.isdisjoint = _set.isdisjoint38 self.issubset = _set.issubset39 self.issuperset = _set.issuperset40 if key is None:41 self._list = SortedList(self._set, load=load)42 else:43 self._list = SortedListWithKey(self._set, key=key, load=load)44 _list = self._list45 self.bisect_left = _list.bisect_left46 self.bisect = _list.bisect47 self.bisect_right = _list.bisect_right48 self.index = _list.index49 self.irange = _list.irange50 self.islice = _list.islice51 if key is not None:52 self.bisect_key_left = _list.bisect_key_left53 self.bisect_key_right = _list.bisect_key_right54 self.bisect_key = _list.bisect_key55 self.irange_key = _list.irange_key56 if iterable is not None:57 self._update(iterable)58 def __contains__(self, value):59 """Return True if and only if *value* is an element in the set."""60 return value in self._set61 def __getitem__(self, index):62 """63 Return the element at position *index*.64 Supports slice notation and negative indexes.65 """66 return self._list[index]67 def __delitem__(self, index):68 """69 Remove the element at position *index*.70 Supports slice notation and negative indexes.71 """72 _set = self._set73 _list = self._list74 if isinstance(index, slice):75 values = _list[index]76 _set.difference_update(values)77 else:78 value = _list[index]79 _set.remove(value)80 del _list[index]81 def _make_cmp(self, set_op, doc):82 "Make comparator method."83 def comparer(self, that):84 "Compare method for sorted set and set-like object."85 # pylint: disable=protected-access86 if isinstance(that, SortedSet):87 return set_op(self._set, that._set)88 elif isinstance(that, Set):89 return set_op(self._set, that)90 else:91 return NotImplemented92 comparer.__name__ = '__{0}__'.format(set_op.__name__)93 doc_str = 'Return True if and only if Set is {0} `that`.'94 comparer.__doc__ = doc_str.format(doc)95 return comparer96 __eq__ = _make_cmp(None, op.eq, 'equal to')97 __ne__ = _make_cmp(None, op.ne, 'not equal to')98 __lt__ = _make_cmp(None, op.lt, 'a proper subset of')99 __gt__ = _make_cmp(None, op.gt, 'a proper superset of')100 __le__ = _make_cmp(None, op.le, 'a subset of')101 __ge__ = _make_cmp(None, op.ge, 'a superset of')102 def __len__(self):103 """Return the number of elements in the set."""104 return len(self._set)105 def __iter__(self):106 """107 Return an iterator over the Set. Elements are iterated in their sorted108 order.109 Iterating the Set while adding or deleting values may raise a110 `RuntimeError` or fail to iterate over all entries.111 """112 return iter(self._list)113 def __reversed__(self):114 """115 Return an iterator over the Set. Elements are iterated in their reverse116 sorted order.117 Iterating the Set while adding or deleting values may raise a118 `RuntimeError` or fail to iterate over all entries.119 """120 return reversed(self._list)121 def add(self, value):122 """Add the element *value* to the set."""123 _set = self._set124 if value not in _set:125 _set.add(value)126 self._list.add(value)127 def clear(self):128 """Remove all elements from the set."""129 self._set.clear()130 self._list.clear()131 def copy(self):132 """Create a shallow copy of the sorted set."""133 return self.__class__(key=self._key, load=self._load, _set=set(self._set))134 __copy__ = copy135 def count(self, value):136 """Return the number of occurrences of *value* in the set."""137 return 1 if value in self._set else 0138 def discard(self, value):139 """140 Remove the first occurrence of *value*. If *value* is not a member,141 does nothing.142 """143 _set = self._set144 if value in _set:145 _set.remove(value)146 self._list.discard(value)147 def pop(self, index=-1):148 """149 Remove and return item at *index* (default last). Raises IndexError if150 set is empty or index is out of range. Negative indexes are supported,151 as for slice indices.152 """153 # pylint: disable=arguments-differ154 value = self._list.pop(index)155 self._set.remove(value)156 return value157 def remove(self, value):158 """159 Remove first occurrence of *value*. Raises ValueError if160 *value* is not present.161 """162 self._set.remove(value)163 self._list.remove(value)164 def difference(self, *iterables):165 """166 Return a new set with elements in the set that are not in the167 *iterables*.168 """169 diff = self._set.difference(*iterables)170 new_set = self.__class__(key=self._key, load=self._load, _set=diff)171 return new_set172 __sub__ = difference173 __rsub__ = __sub__174 def difference_update(self, *iterables):175 """176 Update the set, removing elements found in keeping only elements177 found in any of the *iterables*.178 """179 _set = self._set180 values = set(chain(*iterables))181 if (4 * len(values)) > len(_set):182 _list = self._list183 _set.difference_update(values)184 _list.clear()185 _list.update(_set)186 else:187 _discard = self.discard188 for value in values:189 _discard(value)190 return self191 __isub__ = difference_update192 def intersection(self, *iterables):193 """194 Return a new set with elements common to the set and all *iterables*.195 """196 comb = self._set.intersection(*iterables)197 new_set = self.__class__(key=self._key, load=self._load, _set=comb)198 return new_set199 __and__ = intersection200 __rand__ = __and__201 def intersection_update(self, *iterables):202 """203 Update the set, keeping only elements found in it and all *iterables*.204 """205 _set = self._set206 _list = self._list207 _set.intersection_update(*iterables)208 _list.clear()209 _list.update(_set)210 return self211 __iand__ = intersection_update212 def symmetric_difference(self, that):213 """214 Return a new set with elements in either *self* or *that* but not both.215 """216 diff = self._set.symmetric_difference(that)217 new_set = self.__class__(key=self._key, load=self._load, _set=diff)218 return new_set219 __xor__ = symmetric_difference220 __rxor__ = __xor__221 def symmetric_difference_update(self, that):222 """223 Update the set, keeping only elements found in either *self* or *that*,224 but not in both.225 """226 _set = self._set227 _list = self._list228 _set.symmetric_difference_update(that)229 _list.clear()230 _list.update(_set)231 return self232 __ixor__ = symmetric_difference_update233 def union(self, *iterables):234 """235 Return a new SortedSet with elements from the set and all *iterables*.236 """237 return self.__class__(chain(iter(self), *iterables), key=self._key, load=self._load)238 __or__ = union239 __ror__ = __or__240 def update(self, *iterables):241 """Update the set, adding elements from all *iterables*."""242 _set = self._set243 values = set(chain(*iterables))244 if (4 * len(values)) > len(_set):245 _list = self._list246 _set.update(values)247 _list.clear()248 _list.update(_set)249 else:250 _add = self.add251 for value in values:252 _add(value)253 return self254 __ior__ = update255 _update = update256 def __reduce__(self):257 return (self.__class__, ((), self._key, self._load, self._set))258 @recursive_repr259 def __repr__(self):260 temp = '{0}({1}, key={2}, load={3})'261 return temp.format(262 self.__class__.__name__,263 repr(list(self)),264 repr(self._key),265 repr(self._load)266 )267 def _check(self):268 # pylint: disable=protected-access269 self._list._check()270 assert len(self._set) == len(self._list)271 _set = self._set...
sort.py
Source:sort.py
1#ecoding=utf-82__author__ = 'Administrator'3import random4def rand_list(ran_num=10000):5 list_temp = []6 for i in range(ran_num):7 list_temp.append(random.randint(0, ran_num))8 return list_temp9def list_check(_list):10 for i in range(0, len(_list)-1):11 if _list[i] > _list[i+1]:12 return False13 return True14def select_sort(list2):15 if list2 == [] or len(list2)<2:16 return list217 for i in range(0, len(list2)):18 min = i19 for j in range(i + 1, len(list2)):20 if list2[j] < list2[min]:21 min = j22 list2[i], list2[min] = list2[min], list2[i]23 return list224def insert_sort(list):25 if list == [] or len(list)<2:26 return list27 pass28 for i in range(0, len(list)):29 for j in range(i, 0, -1):30 if list[j] < list[j-1]:31 list[j], list[j-1] = list[j-1], list[j]32 pass33 return list34def mao_pao_sort(_list):35 for i in range(0, len(_list)):36 for j in range(1, len(_list)-i):37 if _list[j-1] > _list[j]:38 _list[j-1], _list[j] = _list[j+1], _list[j]39 pass40 pass41 return list42 pass43def gui_bing_sort2(l_list, r_list):44 l_len = len(l_list)45 r_len = len(r_list)46 #å½éç»ææ¶é´ï¼[]åelse47 if l_len+r_len < 2:48 return l_list+r_list49 l_rst = gui_bing_sort2(l_list[:l_len/2], l_list[l_len/2:l_len])50 r_rst = gui_bing_sort2(r_list[:r_len/2], r_list[r_len/2:r_len])51 lcursor, rcursor = 0, 052 result = []53 min_len = min(l_len, r_len)54 #from litter to bigger55 while lcursor < l_len and rcursor < r_len:56 if l_rst[lcursor] < rcursor[rcursor]:57 result.append(l_rst[lcursor])58 lcursor += 159 else:60 result.append(r_rst[rcursor])61 rcursor += 162 result.extend(l_rst[lcursor])63 result.extend(r_rst[rcursor])64 return result65 pass66def gui_bing_main(_list):67 l_len = len(_list)68 return gui_bing_sort2(_list[:l_len/2], _list[l_len/2:l_len])69def test():70 list_temp = [1, 2, 2, 4, 3]71 list_temp.sort()72 print(list_temp)73if __name__=="__main__":74 # t = rand_list(100)75 # print(t)76 # print(list_check(t))77 # # t2 = select_sort(t)78 # t2 = maopao_sort(t)79 # print(t2)80 # print(list_check(t2))81 # test()82 t = rand_list(5)83 print(t)...
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!!