def search(parent, children, root, T, N): # node is for checking index and its existence purpose # stack is to find index of values array to delete an element whenever len(values) is bigger than 2 # value is for the binary index search purpose node = root total = 0 stack = [node] values = [node] while node: print "node_start",node if children[node]: c = node = children[node].pop() #print node m, x = c - T, c + T #print "m,x",m, x #a binary way to count the possible pairs im = bisect_left(values, m) ix = bisect_right(values, x) #print im, ix, values total += ix - i insort(values, c) #print "values:",values stack.append(c) else: print "stack:",stack values.pop(bisect_left(values, stack.pop())) node = parent[node] print "node:",node print stack,values return total
def format_coord(self, x, y): """ given x,y find z value from the interpolation grid """ extname = self.decam.getSensor(x,y) if extname==None: return 'x=%1.4f, y=%1.4f '%(x, y) else: if self.interpEdges.has_key(extname): xEdges = self.interpEdges[extname][0][0,:] yEdges = self.interpEdges[extname][1][:,0] else: return 'x=%1.4f, y=%1.4f '%(x, y) # get the right index into the interpValues array indY = bisect.bisect_left(yEdges,y)-1 indX = bisect.bisect_left(xEdges,x)-1 if self.interpValues.has_key(extname): shape = self.interpValues[extname].shape if indX>=0 and indX<shape[1] and indY>=0 and indY<shape[0]: z = self.interpValues[extname][indY,indX] return 'x=%1.4f, y=%1.4f, z=%1.4f, %s'%(x, y, z,extname) else: print "problem: ",extname,x,y return 'x=%1.4f, y=%1.4f '%(x, y) else: return 'x=%1.4f, y=%1.4f '%(x, y)
def contains(self, val): """ Returns true if any of the ranges fully enclose the given value, which can be a single value or a Range object Parameters ---------- val : A single value or a Range object Raises ------ ValueError If the value type not compatible with the ranges Returns ------- true if any of the ranges fully enclose the given value """ if len(self) == 0: return False # Get the index+1 of the highest lower cut <= to the value or its # lower cutpoint and check if the value contained if isinstance(val, Range): lower_ind = max(bisect_left(self.lower_cuts, val.lowerCut)-1,0) return self.ranges[lower_ind].encloses(val) else: lower_ind = max(bisect_left(self.lower_cuts, val)-1,0) return self.ranges[lower_ind].contains(val)
def __getitem__(self, key): ''' Overriding the default __getitem__ method with behavior specific to sparse matrices ''' # Your Code if isinstance(key, int): if key >= len(self): return None if len(self.indices) == 0 or len(self.vectors) == 0: return make_vector([0] * self.ncols, lambda x: x == 0) idx = bisect_left(self.indices, key) return (make_vector([0] * self.ncols) if (idx == len(self.indices) or self.indices[idx] != key) else self.vectors[idx]) else: if key[0] >= len(self): return None idx = bisect_left(self.indices, key[0]) return (0 if (idx == len(self.vectors) or self.indices[idx] != key[0]) else self.vectors[idx][key[1]])
def push(self, sources): ''' Push spikes to the queue. Parameters ---------- sources : ndarray of int The indices of the neurons that spiked. ''' if len(sources) and len(self._delays): start = self._source_start stop = self._source_end if start > 0: start_idx = bisect.bisect_left(sources, start) else: start_idx = 0 if stop <= sources[-1]: stop_idx = bisect.bisect_left(sources, stop, lo=start_idx) else: stop_idx = len(sources) + 1 sources = sources[start_idx:stop_idx] if len(sources)==0: return synapse_indices = self._neurons_to_synapses indices = np.concatenate([synapse_indices[source - start] for source in sources]).astype(np.int32) if self._homogeneous: # homogeneous delays self._insert_homogeneous(self._delays[0], indices) else: # vectorise over synaptic events self._insert(self._delays[indices], indices)
def count(self, val): """Return the number of occurrences of *val* in the list.""" _maxes = self._maxes if not _maxes: return 0 pos_left = bisect_left(_maxes, val) if pos_left == len(_maxes): return 0 _lists = self._lists idx_left = bisect_left(_lists[pos_left], val) pos_right = bisect_right(_maxes, val) if pos_right == len(_maxes): return self._len - self._loc(pos_left, idx_left) idx_right = bisect_right(_lists[pos_right], val) if pos_left == pos_right: return idx_right - idx_left right = self._loc(pos_right, idx_right) left = self._loc(pos_left, idx_left) return right - left
def overlaps(self, val): """ Returns true if any of the ranges at least partially overlap the given value, which can be a single value or a Range object Parameters ---------- val : A single value or a Range object Raises: ------- ValueError If the value type not compatible with the ranges Returns ------- true if any of the ranges fully enclose the given value """ if len(self) == 0: return False # Get the index+1 of the highest lower cut <= to the value or its # lower cutpoint and check if the value overlaps if isinstance(val, Range): lower_ind = bisect_left(self.lower_cuts, val.lowerCut)-1 upper_ind = bisect_left(self.lower_cuts, val.upperCut) for i in range(lower_ind,upper_ind): if val.isConnected(self.ranges[i]): if not self.ranges[i].intersection(val).isEmpty(): return True return False else: lower_ind = bisect_left(self.lower_cuts,val)-1 return self.ranges[lower_ind].contains(val)
def add_paths(self, paths): """Add the given URLs to the control paths - a sequence of URLs """ uid = uuid.uuid4() npaths = len(paths) for i, path in enumerate(paths): if i % 100 == 0: cellprofiler.preferences.report_progress( uid, float(i) / npaths, "Loading %s into UI" % path) folder, filename = self.splitpath(path) display_name = urllib2.url2pathname(filename) width, _ = self.GetTextExtent(display_name) idx = bisect.bisect_left(self.folder_names, folder) if idx >= len(self.folder_names) or self.folder_names[idx] != folder: folder_item = self.FolderItem(self, folder) self.folder_names.insert(idx, folder) self.folder_items.insert(idx, folder_item) else: folder_item = self.folder_items[idx] fp = folder_item.filenames pidx = bisect.bisect_left(fp, filename) if pidx >= len(fp) or fp[pidx] != filename: fp.insert(pidx, filename) folder_item.widths.insert(pidx, width) folder_item.file_display_names.insert(pidx, display_name) folder_item.enabled.insert(pidx, True) if len(paths) > 0: cellprofiler.preferences.report_progress(uid, 1, "Done") self.schmutzy = True self.Refresh(eraseBackground=False)
def unregister(self, svc_ref): """ Unregisters a service :param svc_ref: A service reference :return: The unregistered service instance :raise BundleException: Unknown service reference """ with self.__svc_lock: if svc_ref not in self.__svc_registry: raise BundleException("Unknown service: {0}".format(svc_ref)) # Get the owner bundle = self.__svc_bundle.pop(svc_ref) # Get the service instance service = self.__svc_registry.pop(svc_ref) for spec in svc_ref.get_property(OBJECTCLASS): spec_services = self.__svc_specs[spec] # Use bisect to remove the reference (faster) idx = bisect.bisect_left(spec_services, svc_ref) del spec_services[idx] if len(spec_services) == 0: del self.__svc_specs[spec] # Delete bundle association bundle_services = self.__bundle_svc[bundle] idx = bisect.bisect_left(bundle_services, svc_ref) del bundle_services[idx] if len(bundle_services) == 0: # Don't keep empty lists del self.__bundle_svc[bundle] return service
def discard(self, value): _list = self._list _key = self._key(value) _pair = self._pair(_key, value) if self._ordered: _list.discard(_pair) return _maxes = _list._maxes if _maxes is None: return pos = bisect_left(_maxes, _pair) if pos == len(_maxes): return _lists = _list._lists idx = bisect_left(_lists[pos], _pair) len_lists = len(_lists) len_sublist = len(_lists[pos]) while True: pair = _lists[pos][idx] if _key != pair.key: return if value == pair.value: _list._delete(pos, idx) return idx += 1 if idx == len_sublist: pos += 1 if pos == len_lists: return len_sublist = len(_lists[pos]) idx = 0
def ret_domain_cy(a2lat, a2lon, clat, dlatlon): nyTmp, nxTmp = a2lat.shape a1lat = a2lat[:,nxTmp/2] a1lon = a2lon[:,nxTmp/2] idx_latmax = np.argmax(a1lat) a1lat0 = a1lat[:idx_latmax+1] a1lat1 = a1lat[idx_latmax+1:] a1lon0 = a1lon[:idx_latmax+1] a1lon1 = a1lon[idx_latmax+1:] #-- search first half: ascending -- found_domain = 0 idx_c = bisect_left(a1lat0, clat) latTmp = a1lat0[idx_c] lonTmp = a1lon0[idx_c] if (clat-dlatlon<=latTmp)&(latTmp <=clat+dlatlon)&(clon-dlatlon<=lonTmp)&(lonTmp<=clon+dlatlon): found_domain = 1 else: #-- search second half: descending -- idx_c = bisect_left(a1lat1[::-1], clat) idx_c = len(a1lat) - idx_c -1 latTmp = a1lat[idx_c] lonTmp = a1lon[idx_c] if (clat-dlatlon<=latTmp)&(latTmp <=clat+dlatlon)&(clon-dlatlon<=lonTmp)&(lonTmp<=clon+dlatlon): found_domain =1 if found_domain==1: return idx_c else: print 'No matching scans in the target domain are found.' print 'Exit' sys.exit()
def count(self, value): _list = self._list _key = self._key(value) _pair = self._pair(_key, value) if self._ordered: return _list.count(_pair) _maxes = _list._maxes if _maxes is None: return 0 pos = bisect_left(_maxes, _pair) if pos == len(_maxes): return 0 _lists = _list._lists idx = bisect_left(_lists[pos], _pair) total = 0 len_lists = len(_lists) len_sublist = len(_lists[pos]) while True: pair = _lists[pos][idx] if _key != pair.key: return total if value == pair.value: total += 1 idx += 1 if idx == len_sublist: pos += 1 if pos == len_lists: return total len_sublist = len(_lists[pos]) idx = 0
def __contains__(self, value): _list = self._list _key = self._key(value) _pair = self._pair(_key, value) if self._ordered: return _pair in _list _maxes = _list._maxes if _maxes is None: return False pos = bisect_left(_maxes, _pair) if pos == len(_maxes): return False _lists = _list._lists idx = bisect_left(_lists[pos], _pair) len_lists = len(_lists) len_sublist = len(_lists[pos]) while True: pair = _lists[pos][idx] if _key != pair.key: return False if value == pair.value: return True idx += 1 if idx == len_sublist: pos += 1 if pos == len_lists: return False len_sublist = len(_lists[pos]) idx = 0
def _get_offsets(self, starting_sample, ending_sample, num_channels): """ Find the offset to the next zero-crossing, for each channel. """ offsets = [] for zero_index in self.audio.zero_indexes: index = bisect_left(zero_index, starting_sample) - 1 if index < 0: starting_offset = 0 else: starting_crossing = zero_index[index] starting_offset = starting_crossing - starting_sample index = bisect_left(zero_index, ending_sample) if index >= len(zero_index): ending_offset = 0 else: zci = min(bisect_right(zero_index, ending_sample), len(zero_index) - 1) ending_crossing = zero_index[zci] ending_offset = ending_crossing - ending_sample offsets.append((starting_offset, ending_offset)) if num_channels == 1: results = (offsets[0], offsets[0]) elif num_channels == 2: results = (offsets[0], offsets[1]) return results
def threeSumClosest(nums, target): nums.sort() summ = sum(nums[-3:]) if target >= summ: return summ length = len(nums) result = sum(nums[:3]) if result >= target: return result ta = abs(result-target) if ta == 0: return result for i in range(length-2): left = bisect.bisect_left(nums, target - nums[i] - nums[-1], lo = i + 1) #binary search if left > i + 1: left -= 1 for j in range(left, length-1): n3 = target - nums[i] - nums[j] ind = bisect.bisect_left(nums , n3, j+1) values = [ind-1, ind] for val in values: if val > j and val < length: summ = nums[i] + nums[j] + nums[val] if summ == target: return target if abs(summ - target) < ta: result = summ ta = abs(summ - target) if nums[i] + nums[j] + nums[j+1] > target: break return result
def lis_traditional(array): """ If we want to have a list such that list[i] is the smallest ending for a increasing subsequence of size i We can iterate for every num in the array finding where to update to keep the property: 1 -> x1 2 -> x2 num 3 -> x3 We can update to: 1 -> x1 2 -> x2 3 -> num """ M = min(len(array), max(array)) + 1 # smallest_val[i] = smallest ending of a increasing subsequence of len i smallest_val = [M] * M smallest_val[0] = 0 # smallest_idx[i] = index of the smallest ending of an inc subseq of len i smallest_idx = [-1] * M prev = [None] * len(array) for i, num in enumerate(array): index = bisect.bisect_left(smallest_val, num) smallest_val[index] = num smallest_idx[index] = i prev[i] = smallest_idx[index - 1] last = smallest_idx[bisect.bisect_left(smallest_val, M) - 1] ans = [] while last != -1: ans.append(array[last]) last = prev[last] return ans[::-1]
def getCloseGCArrivalsSum(self, gc_ratio, wins): """ Computes sum of sequencing fragments in bins with closest GC ratio """ num_neighbors = self.num_neighbors #binary search the position pos_l = bisect_left(wins, (gc_ratio, 0.)) pos_r = bisect_left(wins, (gc_ratio, float("inf"))) pos = (pos_l + pos_r) / 2 if pos == len(wins): pos -= 1 close_arrivals = wins[pos][1] have = 1 dis = 0 left = pos right = pos+1 while have < num_neighbors: dis += 1 if pos+dis < len(wins): close_arrivals += wins[pos+dis][1] right += 1 have += 1 if pos-dis > 0: close_arrivals += wins[pos-dis][1] left -= 1 have += 1 var = 0. mean = close_arrivals / num_neighbors for x in range(left, right): var += (wins[x][1] - mean)**2 var /= (num_neighbors - 1) return close_arrivals, var
def find_best_price(cp): candidate = 0 if cp > 2 * lowest_price: lowlimit = cp / 2 else: lowlimit = lowest_price larger = cp - lowest_price i = bisect_left(prices, larger) if not larger in count_and_offset: i -= 1 larger = prices[i] while larger >= lowlimit and candidate != cp: smaller = cp - larger if (not smaller in count_and_offset or \ (count_and_offset[smaller] == 1 and cp == 2 * larger)): smaller = prices[bisect_left(prices, smaller) - 1] if smaller < lowest_price: i -= 1 larger = prices[i] continue if smaller + larger > candidate: candidate = smaller + larger i -= 1 larger = prices[i] return candidate
def __sort_registry(self, svc_ref): """ Sorts the registry, after the update of the sort key of given service reference :param svc_ref: A service reference with a modified sort key """ with self.__svc_lock: if svc_ref not in self.__svc_registry: raise BundleException("Unknown service: {0}".format(svc_ref)) # Remove current references bundle_services = self.__bundle_svc[svc_ref.get_bundle()] idx = bisect.bisect_left(bundle_services, svc_ref) del bundle_services[idx] for spec in svc_ref.get_property(OBJECTCLASS): # Use bisect to remove the reference (faster) spec_refs = self.__svc_specs[spec] idx = bisect.bisect_left(spec_refs, svc_ref) del spec_refs[idx] # ... use the new sort key svc_ref.update_sort_key() bisect.insort_left(bundle_services, svc_ref) for spec in svc_ref.get_property(OBJECTCLASS): # ... and insert it again spec_refs = self.__svc_specs[spec] bisect.insort_left(spec_refs, svc_ref)
def get_item(self, i, d=None): """Finds out how many repeats this index implies, then picks strings.""" if i < self.offset_break: by_bisect = bisect.bisect_left(self.offsets, (i, -1), hi=self.index_of_offset) else: by_bisect = bisect.bisect_left(self.offsets, (i, -1), lo=self.index_of_offset) if by_bisect == len(self.offsets) or self.offsets[by_bisect][0] > i: by_bisect -= 1 num = i - self.offsets[by_bisect][0] count = self.offsets[by_bisect][1] if count > 100 and self.content_length < 1000: content = list(self.content) else: content = self.content result = [] if count == 0: return '' for modulus in fastdivmod.divmod_iter(num, self.content_length): result.append(content[modulus]) leftover = count - len(result) if leftover: assert leftover > 0 result.extend([content[0]] * leftover) # smallest place value ends up on the right return ''.join(result[::-1])
def get_two_tk_indices(self, char_pos1, char_pos2): line = bisect.bisect_left(self.newlines, char_pos1) line2 = bisect.bisect_left(self.newlines, char_pos2, line) return ( str(line) + "." + str(char_pos1 - self.newlines[line - 1] - 1), str(line2) + "." + str(char_pos2 - self.newlines[line2 - 1] - 1), )
def __contains__(self, val): """Return True if and only if *val* is an element in the list.""" _maxes = self._maxes if not _maxes: return False key = self._key(val) pos = bisect_left(_maxes, key) if pos == len(_maxes): return False _keys = self._keys _lists = self._lists idx = bisect_left(_keys[pos], key) len_keys = len(_keys) len_sublist = len(_keys[pos]) while True: if _keys[pos][idx] != key: return False if _lists[pos][idx] == val: return True idx += 1 if idx == len_sublist: pos += 1 if pos == len_keys: return False len_sublist = len(_keys[pos]) idx = 0
def midcurves_chain(self): if not self.product: raise Exception("Please set an exchange product first") optr = [] mcsuffix = self.xs.midcurves[1] years = range(int(self.xs.midcurves[0])+1) years.remove(1) ee0 = bisect_left(self.xs.optSerialMonths, EXCH_MONTHS[self.date.month - 1]) optr += [self.make_record('mc', self.xs.optSerialMonths, ee) \ for ee in range(ee0, ee0 + self.xs.numSerialMC)] ee0 += self.xs.numSerialMC while self.date > optr[0].expiry: optr.pop(0) optr.append(self.make_record('mc', self.xs.optSerialMonths, ee0)) ee0 += 1 ee0 = bisect_left(self.xs.optMonths, EXCH_MONTHS[self.date.month-1]) optr += dropwhile(lambda y: y in optr or self.date > y.expiry, \ [self.make_record('mc', self.xs.optMonths, ff) for ff in range(ee0, ee0 + self.xs.numMC)]) midcurves = [_OptR(str(y)+mcsuffix+rr.month, rr.undl[0]+str(int(rr.undl[1])+y+1-(y>0)), rr.expiry) \ for rr in optr for y in years] return {mc.month: mc for mc in midcurves}
def Delete(self, name): """Deletes a task from the store by name. Args: name: the name of the task to delete. Returns: TaskQueueServiceError.UNKNOWN_TASK: if the task is unknown. TaskQueueServiceError.INTERNAL_ERROR: if the store is corrupted. TaskQueueServiceError.OK: otherwise. """ pos = bisect.bisect_left(self._sorted_by_name, (name,)) if pos >= len(self._sorted_by_name): return taskqueue_service_pb.TaskQueueServiceError.UNKNOWN_TASK if self._sorted_by_name[pos][1].task_name() != name: logging.info('looking for task name %s, got task name %s', name, self._sorted_by_name[pos][1].task_name()) return taskqueue_service_pb.TaskQueueServiceError.UNKNOWN_TASK old_task = self._sorted_by_name.pop(pos)[1] eta = old_task.eta_usec() pos = bisect.bisect_left(self._sorted_by_eta, (eta, name, None)) if self._sorted_by_eta[pos][2] is not old_task: logging.error('task store corrupted') return taskqueue_service_pb.TaskQueueServiceError.INTERNAL_ERROR self._sorted_by_eta.pop(pos) return taskqueue_service_pb.TaskQueueServiceError.OK
def options_chain(self): if not self.product: raise RuntimeError("Please set an exchange product first") exps = [] if pd.notnull(self.xs.optSerialMonths): ee0 = bisect_left(self.xs.optSerialMonths, EXCH_MONTHS[self.date.month - 1]) exps += [self.make_record('opt', self.xs.optSerialMonths, ee) \ for ee in range(ee0, ee0 + self.xs.numSerialOpts)] # pop the first if it's past, otherwise pop the last #exps.pop(0 if (self.date > exps[0].expiry) else -1) # Remove and replace expired months ee0 += self.xs.numSerialOpts while self.date > exps[0].expiry: exps.pop(0) exps.append(self.make_record('opt', self.xs.optSerialMonths, ee0)) ee0 += 1 ee0 = bisect_left(self.xs.optMonths, EXCH_MONTHS[self.date.month-1]) exps += dropwhile(lambda y: y in exps or self.date > y.expiry, \ [self.make_record('opt', self.xs.optMonths, ff) for ff in range(ee0, ee0 + self.xs.numOpts + 1)]) #for ee in exps: # print ee return {ee.month: ee for ee in exps}
def is_good_pair(self, n1, n2): p1, p2 = self.p[n1 - 1], self.p[n2 - 1] diff = p2 - p1 ret = True if self.p[bisect.bisect_left(self.p, diff, lo=0, hi=n2)] != diff: ret = False # return False else: pass #print('{} and {} have difference {}'.format(p1, p2, diff)) s = p2 + p1 if s > self.p[-1]: self.extend_numbers_while_lt(s) if s != self.p[-1]: ret = False else: pass #print('{} and {} have sum {}'.format(p1, p2, s)) else: if self.p[bisect.bisect_left(self.p, s, lo=n2)] != s: ret = False else: pass #print('{} and {} have sum {}'.format(p1, p2, s)) return ret
def Lookup(self, maximum, name=None, eta=None): """Lookup a number of sorted tasks from the store. If 'eta' is specified, the tasks are looked up in a list sorted by 'eta', then 'name'. Otherwise they are sorted by 'name'. We need to be able to sort by 'eta' and 'name' because tasks can have identical eta. If you had 20 tasks with the same ETA, you wouldn't be able to page past them, since the 'next eta' would give the first one again. Names are unique, though. Args: maximum: the maximum number of tasks to return. name: a task name to start with. eta: an eta to start with. Returns: A list of up to 'maximum' tasks. Raises: ValueError: if the task store gets corrupted. """ if eta is None: pos = bisect.bisect_left(self._sorted_by_name, (name,)) tasks = (x[1] for x in self._sorted_by_name[pos:pos + maximum]) return list(tasks) if name is None: raise ValueError('must supply name or eta') pos = bisect.bisect_left(self._sorted_by_eta, (eta, name)) tasks = (x[2] for x in self._sorted_by_eta[pos:pos + maximum]) return list(tasks)
def count(self, val): """Return the number of occurrences of *val* in the list.""" _maxes = self._maxes if not _maxes: return 0 key = self._key(val) pos = bisect_left(_maxes, key) if pos == len(_maxes): return 0 _keys = self._keys _lists = self._lists idx = bisect_left(_keys[pos], key) total = 0 len_keys = len(_keys) len_sublist = len(_keys[pos]) while True: if _keys[pos][idx] != key: return total if _lists[pos][idx] == val: total += 1 idx += 1 if idx == len_sublist: pos += 1 if pos == len_keys: return total len_sublist = len(_keys[pos]) idx = 0
def correlationInRange(x, y, srange): ''' Find the correlation of all scores where x is in the range of [score[0], score[1]) Args: x: set of scores y: set of scores in 1-1 correspondence with x srange: tuple consiting of (minS, maxS), which are the minimum and maximum scores in x which should be considered ''' import bisect minS, maxS = srange minI = bisect.bisect_left(x, minS) maxI = bisect.bisect_left(x, maxS) xs = x[minI:maxI] ys = y[minI:maxI] pr = sp.stats.pearsonr(xs, ys)[0] sr = sp.stats.spearmanr(xs, ys)[0] return pr, sr
def _find_index(point, mesh): """ Find the index of the cell that has point inside it. """ x1, x2, y1, y2, z1, z2 = mesh.bounds nz, ny, nx = mesh.shape xs = mesh.get_xs() ys = mesh.get_ys() zs = mesh.get_zs() x, y, z = point if (x <= x2 and x >= x1 and y <= y2 and y >= y1 and ((z <= z2 and z >= z1 and mesh.zdown) or (z >= z2 and z <= z1 and not mesh.zdown))): if mesh.zdown: # -1 because bisect gives the index z would have. I want to know # what index z comes after k = bisect.bisect_left(zs, z) - 1 else: # If z is not positive downward, zs will not be sorted k = len(zs) - bisect.bisect_left(zs[::-1], z) j = bisect.bisect_left(ys, y) - 1 i = bisect.bisect_left(xs, x) - 1 seed = i + j*nx + k*nx*ny # Check if the cell is not masked (topography) if mesh[seed] is not None: return seed return None
def cron(cron_string, start=None, stop=None, second=0): '''Return a schedule generator from a cron-style string. cron_string is a cron-style time expression consisting of five whitespace-separated fields explained in further detail below. start and stop are used to bound the schedule and can be None, datetime.datetime or datetime.timedelta objects or numeric values, such as is returned by time.time(). If start is None, the current time is used. If it is a timedelta, it will be added to the current time. If stop is None, cron will generate values infinitely. If it is a timedelta, the end time is the start time plus stop. Each iteration yields a datetime.datetime object. Since the smallest cron unit is a minute, second may be passed in to offset the time within the minute. The following description of the cron fields is taken from the crontab(5) man page (with slight modifications). The time and date fields are: field allowed values ----- -------------- minute 0-59 hour 0-23 day of month 1-31 month 1-12 (or names, see below) day of week 0-7 (0 or 7 is Sunday, or use names) A field may contain an asterisk (*), which always stands for "first-last". Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an 'hours' entry specifies execution at hours 8, 9, 10, and 11. If the range start or end value is left off, the first or last value will be used. For example, -8 for an 'hours' entry is equivalent to 0-8, 20- for a 'days of month' entry is equivalent to 20-31, and - for a 'months' entry is equivalent to 1-12. Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12". Step values can be used in conjunction with ranges. Following a range with "/<number>" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the 'hours' field to specify every other hour. Step values are also permitted after an asterisk, "*/2" in the 'hours' field is equivalent to "0-23/2". Names can also be used for the 'month' and 'day of week' fields. Use at least the first three letters of the particular day or month (case does not matter). Note: The day can be specified in the following two fields: 'day of month', and 'day of week'. If both fields are restricted (i.e., do not contain the "*" character), then both are used to compute date/time values. For example, "30 4 1,15 * 5" is interpreted as "4:30 am on the 1st and 15th of each month, plus every Friday." ''' if not 0 <= second < 60: raise ValueError('second is out of the range [0, 59]') minutes, hours, days, months, weekdays = parse_cron_string(cron_string) # Convert 0-Sunday to 7-Sunday to match datetime.isoweekday() if weekdays and weekdays[0] == 0: weekdays = weekdays[1:] + (() if weekdays[-1] == 7 else (7,)) # Check that there are some valid month/day combinations. if months and days and not weekdays: unsafe = set([(2, 30), (2, 31), (4, 31), (6, 31), (9, 31), (11, 31)]) combos = set([(m, d) for m in months for d in days]) if not combos - unsafe: raise ValueError('given months and days produce only ' 'impossible combinations') start, stop = _start_stop(start, stop) # Default fields to full range of values months = months or list(range(1, 13)) hours = hours or list(range(0, 24)) minutes = minutes or list(range(0, 60)) def _weekdays(year, month, day=1): '''Iterate over all the days in weekdays for the given year and month starting with day. ''' dt = date(year, month, day) weekday = dt.isoweekday() i = bisect_left(weekdays, weekday) dt += timedelta(weekdays[i] - weekday if i < len(weekdays) else weekdays[0] + 7 - weekday) day, weekday = dt.day, dt.isoweekday() for next in chain(weekdays[i + 1:], cycle(weekdays)): if day > 31: break yield day day, weekday = day + (next - weekday if next > weekday else next + 7 - weekday), next # Handle special case when the start month and day are in the set if start.month in months: if (not (days or weekdays) or days and start.day in days or weekdays and start.isoweekday() in weekdays): if start.hour in hours: for minute in minutes[bisect_right(minutes, start.minute):]: yield datetime(start.year, start.month, start.day, start.hour, minute, second) for hour in hours[bisect_right(hours, start.hour):]: for minute in minutes: yield datetime(start.year, start.month, start.day, hour, minute, second) first_month = [(start.year, start.month, start.day + 1)] else: first_month = [] # Iterate over all values until stop is hit for year, month, first_day in chain(first_month, ((start.year, m, 1) for m in months[bisect_right(months, start.month):]), ((y, m, 1) for y in count(start.year + 1) for m in months)): try: if days: _days = days[bisect_left(days, first_day):] if weekdays: _days = merge(_days, _weekdays(year, month, first_day)) elif weekdays: _days = _weekdays(year, month, first_day) else: _days = list(range(first_day, 32)) for day in _days: for hour in hours: for minute in minutes: dt = datetime(year, month, day, hour, minute, second) if stop and dt > stop: return yield dt except ValueError: pass
s = [0] * (n + 1) a = [0] * (n + 1) a[0] = (0, 0) for i in range(n): s[i + 1] = s[i] - 3 * (S[i] == '1') + 2 * (S[i] == '0') a[i + 1] = (s[i + 1], i + 1) a.sort() dp = [0] * (n + 1) dp[0] = a[0][1] for i in range(n): dp[i + 1] = max(dp[i], a[i + 1][1]) answer = 0 for i in range(n): dx = a[0][0] - s[i] if s[n] <= 0: x = K - 1 if dx + s[n] * x > 0: continue elif dx > 0: continue else: x = min(K - 1, -dx // s[n]) v = x * s[n] - s[i] p = bisect_left(a, (-v + 1, 0)) - 1 answer = max(answer, dp[p] - i + x * n) print(answer)
def DaysToExpire(self, today, contractIndex): index = bisect.bisect_left(self.expiry, today) return (self.expiry[index + contractIndex].date() - today).days
def Initialize(self): self.SetStartDate(2011, 1, 1) # Set Start Date self.SetEndDate(2014, 12, 31) # Set End Date self.SetCash(100000000) # Set Strategy Cash # if the weight of a futures contract is lower than this, we will not trade self.thresholdToPlaceOrder = 0.001 self.multipler = 1000 # VIX multipler is $1000 # transaction fee self.fee = 0.05 # we use the implied vol from the call options as the mean vol doesn't always exist, e.g. 2014/02/14 self.vixvolColPrefix = "ivcall" # we don't trade on weekends and holidays holidays = self.TradingCalendar.GetDaysByType( TradingDayType.PublicHoliday, self.StartDate, self.EndDate) self.holidays = [i.Date.date() for i in holidays] self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin) # the token is purchased from Quandl with a monthly cost, will need to be updated if it expires. Quandl.SetAuthCode("WFW5X2RC9YiGoVUbbsX_") self.vxxvol = self.AddData(QuandlVxx, "VOL/VXX", Resolution.Daily, TimeZones.NewYork).Symbol self.vixvol = self.AddData(QuandlVxx, "VOL/VIX", Resolution.Daily, TimeZones.NewYork).Symbol # vix data self.Download("http://cache.quantconnect.com/alternative/cboe/vix.csv") self.vix = self.AddData(CBOE, "VIX", Resolution.Daily).Symbol # VX9 misses 01/23 - 03/23 in 2015, exclude vx9 from modeling # need 8 monthly futures because when calculating the weights I will exclude the current front month self.nfut = 7 self.nexclude = 0 self.VIX_futures_names = [ "VX" + str(i) for i in range(1, 1 + self.nfut) ] self.VIX_symbols = [] for vname in self.VIX_futures_names: self.VIX_symbols.append( self.AddData(QuandlFutures, "CHRIS/CBOE_" + vname, Resolution.Daily).Symbol) # the data of VXX or vxx.1 don't match with online sources (e.g. yahoo) after 2018 # https://www.quantconnect.com/forum/discussion/7975/can-039-t-load-historical-vxx-data-even-though-it-looks-like-it-exists/p1 # use this ticker before 2019 self.vxx = self.AddEquity( "vxx.1", Resolution.Daily ).Symbol # Add VXX, vxx.1 is the permtick according to the link # use this ticker after 2018 #self.vxx = self.AddEquity("VXX", Resolution.Daily).Symbol vixfuture = self.AddFuture(Futures.Indices.VIX) vixfuture.SetFilter(timedelta(0), timedelta(300)) # 2011 to present expiry_f = self.Download( "https://www.dropbox.com/s/ny8nxqcp6u76igw/expiry.csv?dl=1") self.expiry = pd.read_csv( StringIO(expiry_f), names=["expiry"], parse_dates=["expiry"], infer_datetime_format=True)["expiry"].tolist() #stores the map from expiry to the contract self.contracts = {} # index of the front contract self.frontMonthIndex = bisect.bisect_left(self.expiry, self.StartDate) # weight for vix future contracts and vxx (the last one) self.curWeight = np.array([0.] * (self.nfut - self.nexclude + 1)) if self.frontMonthIndex == 0: self.Log("start date is earlier than available data, quiting...") self.Quit() # some dates have bad data, and mess up the backtesting, exclude them in the list # the hacky way to get around these bad days is to liquidate before these days, and # then buy back after these days self.bad_dates = [date(2012,11,5), date(2012,11,6), date(2012,11,7),\ date(2013,10,10),date(2013,10,28),date(2013,10,29), date(2013,10,30),date(2013,10,31), date(2014,10,15)] # we trade at 10am ET every day self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(10, 0), self.TryTrade)
def locate(self,vaddr): p = self.__cache if vaddr in p: return p.index(vaddr) i = bisect_left(p,vaddr) if i==0: return None else: return i-1
import bisect N = int(input()) W = [int(input()) for l in range(N)] box = [W[0]] for i in range(1, N): if max(box) < W[i]: box.append(W[i]) else: box[bisect.bisect_left(box, W[i])] = W[i] print(len(box))
def get(self, timestamp): """Get the resource sample nearest to the given timestamp""" closest = min(bisect_left(self._values, (timestamp, None)), len(self) - 1) return self._values[closest]
def _box_points(values, mode='extremes'): """ Default mode: (mode='extremes' or unset) Return a 7-tuple of 2x minimum, Q1, Median, Q3, and 2x maximum for a list of numeric values. 1.5IQR mode: (mode='1.5IQR') Return a 7-tuple of min, Q1 - 1.5 * IQR, Q1, Median, Q3, Q3 + 1.5 * IQR and max for a list of numeric values. Tukey mode: (mode='tukey') Return a 7-tuple of min, q[0..4], max and a list of outliers Outliers are considered values x: x < q1 - IQR or x > q3 + IQR SD mode: (mode='stdev') Return a 7-tuple of min, q[0..4], max and a list of outliers Outliers are considered values x: x < q2 - SD or x > q2 + SD SDp mode: (mode='pstdev') Return a 7-tuple of min, q[0..4], max and a list of outliers Outliers are considered values x: x < q2 - SDp or x > q2 + SDp The iterator values may include None values. Uses quartile definition from Mendenhall, W. and Sincich, T. L. Statistics for Engineering and the Sciences, 4th ed. Prentice-Hall, 1995. """ def median(seq): n = len(seq) if n % 2 == 0: # seq has an even length return (seq[n // 2] + seq[n // 2 - 1]) / 2 else: # seq has an odd length return seq[n // 2] def mean(seq): return sum(seq) / len(seq) def stdev(seq): m = mean(seq) l = len(seq) v = sum((n - m)**2 for n in seq) / (l - 1) # variance return v**0.5 # sqrt def pstdev(seq): m = mean(seq) l = len(seq) v = sum((n - m)**2 for n in seq) / l # variance return v**0.5 # sqrt outliers = [] # sort the copy in case the originals must stay in original order s = sorted([x for x in values if x is not None]) n = len(s) if not n: return (0, 0, 0, 0, 0, 0, 0), [] elif n == 1: return (s[0], s[0], s[0], s[0], s[0], s[0], s[0]), [] else: q2 = median(s) # See 'Method 3' in http://en.wikipedia.org/wiki/Quartile if n % 2 == 0: # even q1 = median(s[:n // 2]) q3 = median(s[n // 2:]) else: # odd if n == 1: # special case q1 = s[0] q3 = s[0] elif n % 4 == 1: # n is of form 4n + 1 where n >= 1 m = (n - 1) // 4 q1 = 0.25 * s[m - 1] + 0.75 * s[m] q3 = 0.75 * s[3 * m] + 0.25 * s[3 * m + 1] else: # n is of form 4n + 3 where n >= 1 m = (n - 3) // 4 q1 = 0.75 * s[m] + 0.25 * s[m + 1] q3 = 0.25 * s[3 * m + 1] + 0.75 * s[3 * m + 2] iqr = q3 - q1 min_s = s[0] max_s = s[-1] if mode == 'extremes': q0 = min_s q4 = max_s elif mode == 'tukey': # the lowest datum still within 1.5 IQR of the lower quartile, # and the highest datum still within 1.5 IQR of the upper # quartile [Tukey box plot, Wikipedia ] b0 = bisect_left(s, q1 - 1.5 * iqr) b4 = bisect_right(s, q3 + 1.5 * iqr) q0 = s[b0] q4 = s[b4 - 1] outliers = s[:b0] + s[b4:] elif mode == 'stdev': # one standard deviation above and below the mean of the data sd = stdev(s) b0 = bisect_left(s, q2 - sd) b4 = bisect_right(s, q2 + sd) q0 = s[b0] q4 = s[b4 - 1] outliers = s[:b0] + s[b4:] elif mode == 'pstdev': # one population standard deviation above and below # the mean of the data sdp = pstdev(s) b0 = bisect_left(s, q2 - sdp) b4 = bisect_right(s, q2 + sdp) q0 = s[b0] q4 = s[b4 - 1] outliers = s[:b0] + s[b4:] elif mode == '1.5IQR': # 1.5IQR mode q0 = q1 - 1.5 * iqr q4 = q3 + 1.5 * iqr return (min_s, q0, q1, q2, q3, q4, max_s), outliers
from bisect import bisect_left n = int(input()) s = list(map(int, input().split())) q = int(input()) t = list(map(int, input().split())) ans = 0 for ele in t: i = bisect_left(s, ele) if s[i] == ele: ans += 1 print(ans)
def plot_volume_open_interest( ticker: str, expiry: str, min_sp: float, max_sp: float, min_vol: float, export: str = "", ): """Plot volume and open interest Parameters ---------- ticker: str Stock ticker expiry: str Option expiration min_sp: float Min strike price max_sp: float Max strike price min_vol: float Min volume to consider export: str Format for exporting data """ current_price = tradier_model.last_price(ticker) options = tradier_model.get_option_chains(ticker, expiry) calls = options[options.option_type == "call"][[ "strike", "volume", "open_interest" ]] puts = options[options.option_type == "put"][[ "strike", "volume", "open_interest" ]] # Process Calls Data df_calls = calls.pivot_table(index="strike", values=["volume", "open_interest"], aggfunc="sum").reindex() df_calls["strike"] = df_calls.index df_calls["type"] = "calls" df_calls["open_interest"] = df_calls["open_interest"] df_calls["volume"] = df_calls["volume"] df_calls["oi+v"] = df_calls["open_interest"] + df_calls["volume"] df_calls["spot"] = round(current_price, 2) df_puts = puts.pivot_table(index="strike", values=["volume", "open_interest"], aggfunc="sum").reindex() df_puts["strike"] = df_puts.index df_puts["type"] = "puts" df_puts["open_interest"] = df_puts["open_interest"] df_puts["volume"] = -df_puts["volume"] df_puts["open_interest"] = -df_puts["open_interest"] df_puts["oi+v"] = df_puts["open_interest"] + df_puts["volume"] df_puts["spot"] = round(current_price, 2) call_oi = calls.set_index("strike")["open_interest"] / 1000 put_oi = puts.set_index("strike")["open_interest"] / 1000 df_opt = pd.merge(call_oi, put_oi, left_index=True, right_index=True) df_opt = df_opt.rename(columns={ "open_interest_x": "OI_call", "open_interest_y": "OI_put" }) max_pain = op_helpers.calculate_max_pain(df_opt) if min_vol == -1 and min_sp == -1 and max_sp == -1: # If no argument provided, we use the percentile 50 to get 50% of upper volume data volume_percentile_threshold = 50 min_vol_calls = np.percentile(df_calls["oi+v"], volume_percentile_threshold) min_vol_puts = np.percentile(df_puts["oi+v"], volume_percentile_threshold) df_calls = df_calls[df_calls["oi+v"] > min_vol_calls] df_puts = df_puts[df_puts["oi+v"] < min_vol_puts] else: if min_vol > -1: df_calls = df_calls[df_calls["oi+v"] > min_vol] df_puts = df_puts[df_puts["oi+v"] < -min_vol] if min_sp > -1: df_calls = df_calls[df_calls["strike"] > min_sp] df_puts = df_puts[df_puts["strike"] > min_sp] if max_sp > -1: df_calls = df_calls[df_calls["strike"] < max_sp] df_puts = df_puts[df_puts["strike"] < max_sp] if df_calls.empty and df_puts.empty: print( "The filtering applied is too strong, there is no data available for such conditions.\n" ) return # Initialize the matplotlib figure _, ax = plt.subplots(figsize=plot_autoscale(), dpi=cfp.PLOT_DPI) # make x axis symmetric axis_origin = max(abs(max(df_puts["oi+v"])), abs(max(df_calls["oi+v"]))) ax.set_xlim(-axis_origin, +axis_origin) sns.set_style(style="darkgrid") g = sns.barplot( x="oi+v", y="strike", data=df_calls, label="Calls: Open Interest", color="lightgreen", orient="h", ) g = sns.barplot( x="volume", y="strike", data=df_calls, label="Calls: Volume", color="green", orient="h", ) g = sns.barplot( x="oi+v", y="strike", data=df_puts, label="Puts: Open Interest", color="pink", orient="h", ) g = sns.barplot( x="volume", y="strike", data=df_puts, label="Puts: Volume", color="red", orient="h", ) # draw spot line s = [float(strike.get_text()) for strike in ax.get_yticklabels()] spot_index = bisect_left( s, current_price) # find where the spot is on the graph spot_line = ax.axhline(spot_index, ls="--", color="dodgerblue", alpha=0.3) # draw max pain line max_pain_index = bisect_left(s, max_pain) max_pain_line = ax.axhline(max_pain_index, ls="-", color="black", alpha=0.3) max_pain_line.set_linewidth(3) # format ticklabels without - for puts g.set_xticks(g.get_xticks()) xlabels = [f"{x:,.0f}".replace("-", "") for x in g.get_xticks()] g.set_xticklabels(xlabels) plt.title( f"{ticker} volumes for {expiry} (open interest displayed only during market hours)" ) ax.invert_yaxis() _ = ax.legend() handles, _ = ax.get_legend_handles_labels() handles.append(spot_line) handles.append(max_pain_line) # create legend labels + add to graph labels = [ "Calls open interest", "Calls volume ", "Puts open interest", "Puts volume", "Current stock price", f"Max pain = {max_pain}", ] plt.legend(handles=handles[:], labels=labels) sns.despine(left=True, bottom=True) if gtff.USE_ION: plt.ion() plt.show() export_data( export, os.path.dirname(os.path.abspath(__file__)), "voi_tr", options, ) print("")
from bisect import bisect_left n = int(input()) array = [] cnt = 0 for i in range(n): num = int(input()) cnt += 1 index = bisect_left(array, num) array.insert(index, num) if cnt % 2 != 0: print(array[cnt // 2]) else: print(min(array[cnt // 2 - 1], array[cnt // 2]))
sm[i] = sm[i-1] - b ps[i] = ps[i-1] fs[i] = fs[i-1] + 1 X = min(X, sm[i]) ts = [x[0] for x in A] if X >= 0: print(ts[-1] + 1) exit(0) for si, s in enumerate(ts): # update timerange [ti, ti+l) ti = bisect.bisect_left(ts, s+l) ti -= 1 if 0 <= ti < n: t = ts[ti] if s <= t < s+l: p = ps[ti+1]-ps[si] f = fs[ti+1]-fs[si] diff = p*(c-a) + f*(b-d) if diff >= abs(X): print(s) exit(0) print(-1)
def main(): # Get data activity_metric = GC.activityMetrics() activity = GC.activity(activity=None) zone = GC.athleteZones(date=activity_metric["date"], sport="bike") all_intervals = GC.activityIntervals() selected_type = None if 'power' in activity: if len(all_intervals['type']) > 0: all_intervals = pd.DataFrame(all_intervals) selected_type = determine_selection_type(all_intervals) else: fail_msg = "No intervals found in this activities, possible solutions: <br>" \ "Create manual intervals or enable interval auto-discovery via Tools->Options->Intervals" else: fail_msg = "No power data found in this activities " if selected_type: # Define chart title title = "Average Power per Interval " \ "(CP:" + str(zone["cp"][0]) + ") " + \ "Selected Interval Type=" + str(selected_type) intervals = all_intervals[all_intervals['type'].str.contains(selected_type)] # Identify for every interval the zone color breaks = zone["zoneslow"][0] zone_colors = zone["zonescolor"][0] interval_colors = [] avg_power_pct = [] for interval in intervals["Average_Power"]: index = bisect.bisect_left(breaks, interval) interval_colors.append(zone_colors[index - 1]) avg_power_pct.append(str(round((interval / zone["cp"][0]) * 100, 1)) + "%") # Add percentage labels legend = [] zone_index = 1 for zone in breaks: legend.append("Z" + str(zone_index) + "(" + str(zone) + ")") zone_index += 1 # array of lap names to printed on the x-axis lap_names = np.asarray(intervals["name"]) # array of y values watts_y = np.asarray(intervals["Average_Power"]) # define x-axis (start time of the intervals) x = np.asarray(intervals["start"]) # arrays used for text for every interval distance = np.asarray(intervals["Distance"]) stop = np.asarray(intervals["stop"]) duration = np.asarray(intervals["Duration"]) # duration = [stop - start for stop, start in zip(stop, x)] # define x-axis heart rate heart_rate = np.asarray(list(activity['heart.rate'])) # define x-axis seconds seconds = np.asarray(list(activity['seconds'])) # Start building chart_not_working_yet_after_single_extract fig = go.Figure() add_legend_data(fig, legend, zone_colors) add_default_layout(fig, title, watts_y) if selected_type == "USER" or selected_type == "ALL": add_annotation(fig, x, watts_y, duration, distance, avg_power_pct, lap_names) add_interval_shapes(fig, x, watts_y, duration, lap_names, interval_colors) add_heart_rate_line(fig, seconds, heart_rate) else: x = np.arange(0.5, len(lap_names), 1) add_annotation(fig, x, watts_y, duration, distance, avg_power_pct, lap_names, bar_chart=True) add_interval_bars(fig, x, watts_y, lap_names, interval_colors, selected_type) plotly.offline.plot(fig, auto_open=False, filename=temp_file.name) else: create_unavailable_html(fail_msg) GC.webpage(pathlib.Path(temp_file.name).as_uri())
for prime in primes[greatest:]: consecutive_lst.append(prime) while sum(consecutive_lst) > p: # if our smallest value times our longest length is greater than p, we know the length of p must be shorter if consecutive_lst.popleft() >= p // greatest: # we no longer care what value is returned as long as it is less than greatest return 1 # the first time our list sums to p, return, as this is the longest the sum could be if sum(consecutive_lst) == p: return len(consecutive_lst) return 1 # our consecutive sum is only p max_val = 10**6 primes = find_primes_less_than(max_val) longest = 0 longest_len = 1 # rough estimate that our solution is between 950000 and 1000000 for i in primes[bisect.bisect_left(primes, max_val // 100 * 95):]: p_sum = consecutive_prime_sum(i, longest_len) if p_sum > longest_len: longest_len, longest = p_sum, i print(longest) # 997651, p_sum == 543
def get_cache_node(self, source_key: str): """Based on the source_key, determine which cache node to cache""" key_hash = self.generate_hash(source_key) index = bisect.bisect_left(self.cache_list, key_hash) % len( self.cache_list) return self.cache_node[self.cache_list[index]]
def __init__(self, words, threshold, start_symbol=True, with_counts=False, shelfname=None): if not with_counts: words = [(1, w) for w in words] self.start = start = '\0' if start_symbol else '' self.end = end = '\0' lendelta = len(start) + len(end) words = [(len(w) + lendelta, (c, start + w + end)) for c, w in words] self.nodes = nodes = self.setup_nodes(shelfname) words.sort(key=operator.itemgetter(0)) if not words: return lens, words = zip(*words) lens = numpy.array(lens) nwords = len(words) def zerodict(): return collections.defaultdict(itertools.repeat(0).__next__) charcounts = zerodict() for count, word in words: for c in word[start_symbol:]: charcounts[c] += count totchars = sum(charcounts.values()) transitions, counts = zip(*sorted(charcounts.items(), key=operator.itemgetter(1), reverse=True)) transitions = ''.join(transitions) counts = numpy.array(counts) totchars = counts.sum() probabilities = counts / totchars if transitions: nodes[''] = TmpNode(transitions, probabilities, probabilities.cumsum(), -numpy.log2(probabilities)) leftidx = 0 skipwords = set() for n in range(2, lens[-threshold] + 1): leftidx = bisect.bisect_left(lens, n) ngram_counter = zerodict() for i in range(leftidx, nwords): if i in skipwords: continue count, word = words[i] skip = True for j in range(lens[i] - n + 1): ngram = word[j: j + n] if ngram[:-2] in nodes: ngram_counter[ngram] += count skip = False if skip: skipwords.add(i) tmp_dict = collections.defaultdict(list) for ngram, count in ngram_counter.items(): tmp_dict[ngram[:-1]].append((ngram[-1], count)) for state, sscounts in tmp_dict.items(): total = sum(count for _, count in sscounts) if total < threshold: continue trans_probs = {c: count / total for c, count in sscounts if count >= threshold} missing = 1 - sum(trans_probs.values()) if missing == 1: continue if missing > 0: parent_state = self.nodes[state[1:]] for c, p in zip(parent_state.transitions, parent_state.probabilities): trans_probs[c] = trans_probs.get(c, 0) + p * missing trans_probs = sorted(trans_probs.items(), key=operator.itemgetter(1), reverse=True) transitions, probabilities = zip(*trans_probs) transitions = ''.join(transitions) probabilities = numpy.array(probabilities) # probabilities must sum to 1 # assert abs(probabilities.sum() - 1) < 0.001 nodes[state] = TmpNode(transitions, probabilities, probabilities.cumsum(), -numpy.log2(probabilities)) Node = ngram_chain.Node for state, node in self.nodes.items(): nodes[state] = Node(node.transitions, node.cumprobs, node.logprobs)
def searchInsert(self, nums: List[int], target: int) -> int: return bisect_left(nums, target)
def binary_search(self, arr, x, low=0, high=None): # can't use arr to specify default for high high = high if high is not None else len(arr) # high defaults to len(arr) pos = bisect_left(arr,x,low,high) # find insertion position return (pos if pos != high and arr[pos] == x else -1) # don't walk off the end
#푼날짜 5/25 from bisect import bisect_left _ = int(input()) nlis = list(map(int, input().split())) nlis.sort() nlis.append(10000001) m = int(input()) mlis = list(map(int, input().split())) smlis = list(set(mlis)) dic = {} table = [] for i in range(len(smlis)): dic[smlis[i]] = 0 for i in range(len(smlis)): a = bisect_left(nlis, smlis[i]) if a >= len(nlis): y = 1 elif nlis[a] == smlis[i]: cnt = 1 b = a + 1 while nlis[b] == smlis[i]: b += 1 cnt += 1 dic[smlis[i]] = cnt else: y = 1 for i in range(m): table.append(dic[mlis[i]]) print(*table)
def get_index(x_lst, x_max): if x_max is None: return len(x_lst) return bisect_left(x_lst, x_max)
def writeAnnots(f, chartTimes, annots, defaultSeries): if len(chartTimes) == 0: return # First, just aggregate by the timeStamp we need to attach each annot to, so in # case more than one change lands on one point, we can show all of them under a # single annot: if type(chartTimes) is list: firstTimeStamp = chartTimes[0] else: firstTimeStamp = None for l in chartTimes.values(): if len(l) > 0 and (firstTimeStamp is None or l[0] < firstTimeStamp): firstTimeStamp = l[0] label = 0 byTimeStamp = {} for annot in annots: date, reason = annot[:2] if len(annot) > 2: series = annot[2] else: series = defaultSeries parts = list(int(x) for x in date.split('-')) year, month, day = parts[:3] if len(parts) == 6: hour, min, sec = parts[3:] else: if len(parts) != 3: raise RuntimeError( 'invalid timestamp "%s": should be YYYY-MM-DD[-HH-MM-SS]' % date) hour = min = sec = 0 timeStamp = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=min, second=sec) if timeStamp < firstTimeStamp - datetime.timedelta(days=1): # If this annot is from before this chart started, skip it: # print('skip annot %s %s: %s' % (date, series, reason)) continue # Place the annot on the next data point that's <= the annot timestamp, in this chart: if type(chartTimes) is dict: if series not in chartTimes: print('skip annot %s %s: %s (series not in chartTimes: %s)' % (date, series, reason, chartTimes.keys())) continue l = chartTimes[series] else: l = chartTimes idx = bisect.bisect_left(l, timeStamp) if idx is not None and idx < len(l): # This is the timestamp, on or after when the annot was, that exists in the particular # series we are annotating: bestTimeStamp = l[idx] if bestTimeStamp not in byTimeStamp: byTimeStamp[bestTimeStamp] = {} if series not in byTimeStamp[bestTimeStamp]: byTimeStamp[bestTimeStamp][series] = [label] label += 1 byTimeStamp[bestTimeStamp][series].append(reason) # Then render the annots: f.write('g.ready(function() {g.setAnnotations([') for timeStamp, d in byTimeStamp.items(): for series, items in d.items(): messages = r'\n\n'.join(items[1:]) f.write('{series: "%s", x: "%s", shortText: "%s", text: "%s"},\n' % \ (series, toString(timeStamp), getLabel(items[0]), messages.replace('"', '\\"'))) f.write(']);});\n')
def is_present(k): # bisect_left returns the first index of element that is # less than k in list B # O(log(m)) i = bisect.bisect_left(B, k) return i < len(B) and B[i] == k
import sys from bisect import bisect_left N = int(sys.stdin.readline()) arr = list(map(int, sys.stdin.readline().split())) LIS = [] where = [] for i in range(N): if not LIS or arr[i] > LIS[-1]: LIS.append(arr[i]) where.append(len(LIS) - 1) else: tmp = bisect_left(LIS, arr[i]) LIS[tmp] = arr[i] where.append(tmp) print(len(LIS)) check = len(LIS) - 1 answer = [] for i in range(len(arr) - 1, -1, -1): if where[i] == check: answer.append(arr[i]) check -= 1 print(*answer[::-1])
def binarySearch(a, x): i = bisect.bisect_left(a, x) return i < len(a)
def train(self, train_tokens, max_rules=200, min_score=2): # If TESTING is true, extra computation is done to determine whether # each "best" rule actually reduces net error by the score it received. TESTING = False # Basic idea: Keep track of the rules that apply at each position. # And keep track of the positions to which each rule applies. # The set of somewhere-useful rules that apply at each position rulesByPosition = [] for i in range(len(train_tokens)): rulesByPosition.append(set()) # Mapping somewhere-useful rules to the positions where they apply. # Then maps each position to the score change the rule generates there. # (always -1, 0, or 1) positionsByRule = {} # Map scores to sets of rules known to achieve *at most* that score. rulesByScore = {0: {}} # Conversely, map somewhere-useful rules to their minimal scores. ruleScores = {} tagIndices = {} # Lists of indices, mapped to by their tags # Maps rules to the first index in the corpus where it may not be known # whether the rule applies. (Rules can't be chosen for inclusion # unless this value = len(corpus). But most rules are bad, and # we won't need to check the whole corpus to know that.) # Some indices past this may actually have been checked; it just isn't # guaranteed. firstUnknownIndex = {} # Make entries in the rule-mapping dictionaries. # Should be called before _updateRuleApplies. def _initRule(rule): positionsByRule[rule] = {} rulesByScore[0][rule] = None ruleScores[rule] = 0 firstUnknownIndex[rule] = 0 # Takes a somewhere-useful rule which applies at index i; # Updates all rule data to reflect that the rule so applies. def _updateRuleApplies(rule, i): # If the rule is already known to apply here, ignore. # (This only happens if the position's tag hasn't changed.) if positionsByRule[rule].has_key(i): return if rule.replacement_tag() == train_tokens[i][1]: positionsByRule[rule][i] = 1 elif rule.original_tag() == train_tokens[i][1]: positionsByRule[rule][i] = -1 else: # was wrong, remains wrong positionsByRule[rule][i] = 0 # Update rules in the other dictionaries del rulesByScore[ruleScores[rule]][rule] ruleScores[rule] += positionsByRule[rule][i] if not rulesByScore.has_key(ruleScores[rule]): rulesByScore[ruleScores[rule]] = {} rulesByScore[ruleScores[rule]][rule] = None rulesByPosition[i].add(rule) # Takes a rule which no longer applies at index i; # Updates all rule data to reflect that the rule doesn't apply. def _updateRuleNotApplies(rule, i): del rulesByScore[ruleScores[rule]][rule] ruleScores[rule] -= positionsByRule[rule][i] if not rulesByScore.has_key(ruleScores[rule]): rulesByScore[ruleScores[rule]] = {} rulesByScore[ruleScores[rule]][rule] = None del positionsByRule[rule][i] rulesByPosition[i].remove(rule) # Optional addition: if the rule now applies nowhere, delete # all its dictionary entries. tagged_tokens = list( self._initial_tagger.tag(t[0] for t in train_tokens)) # First sort the corpus by tag, and also note where the errors are. errorIndices = [] # only used in initialization for i in range(len(tagged_tokens)): tag = tagged_tokens[i][1] if tag != train_tokens[i][1]: errorIndices.append(i) if not tagIndices.has_key(tag): tagIndices[tag] = [] tagIndices[tag].append(i) print "Finding useful rules..." # Collect all rules that fix any errors, with their positive scores. for i in errorIndices: for template in self._templates: # Find the templated rules that could fix the error. for rule in template.applicable_rules(tagged_tokens, i, train_tokens[i][1]): if not positionsByRule.has_key(rule): _initRule(rule) _updateRuleApplies(rule, i) print "Done initializing %i useful rules." % len(positionsByRule) if TESTING: after = -1 # bug-check only # Each iteration through the loop tries a new maxScore. maxScore = max(rulesByScore.keys()) rules = [] while len(rules) < max_rules and maxScore >= min_score: # Find the next best rule. This is done by repeatedly taking a rule with # the highest score and stepping through the corpus to see where it # applies. When it makes an error (decreasing its score) it's bumped # down, and we try a new rule with the highest score. # When we find a rule which has the highest score AND which has been # tested against the entire corpus, we can conclude that it's the next # best rule. bestRule = None bestRules = rulesByScore[maxScore].keys() for rule in bestRules: # Find the first relevant index at or following the first # unknown index. (Only check indices with the right tag.) ti = bisect.bisect_left(tagIndices[rule.original_tag()], firstUnknownIndex[rule]) for nextIndex in tagIndices[rule.original_tag()][ti:]: if rule.applies(tagged_tokens, nextIndex): _updateRuleApplies(rule, nextIndex) if ruleScores[rule] < maxScore: firstUnknownIndex[rule] = nextIndex + 1 break # the _update demoted the rule # If we checked all remaining indices and found no more errors: if ruleScores[rule] == maxScore: firstUnknownIndex[rule] = len( tagged_tokens) # i.e., we checked them all print "%i) %s (score: %i)" % (len(rules) + 1, rule, maxScore) bestRule = rule break if bestRule == None: # all rules dropped below maxScore del rulesByScore[maxScore] maxScore = max(rulesByScore.keys()) continue # with next-best rules # bug-check only if TESTING: before = len(_errorPositions(tagged_tokens, train_tokens)) print "There are %i errors before applying this rule." % before assert after == -1 or before == after, \ "after=%i but before=%i" %(after,before) print "Applying best rule at %i locations..." \ %len(positionsByRule[bestRule].keys()) # If we reach this point, we've found a new best rule. # Apply the rule at the relevant sites. # (apply_at is a little inefficient here, since we know the rule applies # and don't actually need to test it again.) rules.append(bestRule) bestRule.apply_at(tagged_tokens, positionsByRule[bestRule].keys()) # Update the tag index accordingly. for i in positionsByRule[bestRule].keys(): # where it applied # Update positions of tags # First, find and delete the index for i from the old tag. oldIndex = bisect.bisect_left( tagIndices[bestRule.original_tag()], i) del tagIndices[bestRule.original_tag()][oldIndex] # Then, insert i into the index list of the new tag. if not tagIndices.has_key(bestRule.replacement_tag()): tagIndices[bestRule.replacement_tag()] = [] newIndex = bisect.bisect_left( tagIndices[bestRule.replacement_tag()], i) tagIndices[bestRule.replacement_tag()].insert(newIndex, i) # This part is tricky. # We need to know which sites might now require new rules -- that # is, which sites are close enough to the changed site so that # a template might now generate different rules for it. # Only the templates can know this. # # If a template now generates a different set of rules, we have # to update our indices to reflect that. print "Updating neighborhoods of changed sites.\n" # First, collect all the indices that might get new rules. neighbors = set() for i in positionsByRule[bestRule].keys(): # sites changed for template in self._templates: neighbors.update( template.get_neighborhood(tagged_tokens, i)) # Then collect the new set of rules for each such index. c = d = e = 0 for i in neighbors: siteRules = set() for template in self._templates: # Get a set of the rules that the template now generates siteRules.update( set( template.applicable_rules(tagged_tokens, i, train_tokens[i][1]))) # Update rules no longer generated here by any template for obsolete in rulesByPosition[i] - siteRules: c += 1 _updateRuleNotApplies(obsolete, i) # Update rules only now generated by this template for newRule in siteRules - rulesByPosition[i]: d += 1 if not positionsByRule.has_key(newRule): e += 1 _initRule(newRule) # make a new rule w/score=0 _updateRuleApplies(newRule, i) # increment score, etc. if TESTING: after = before - maxScore print "%i obsolete rule applications, %i new ones, " %(c,d)+ \ "using %i previously-unseen rules." %e maxScore = max(rulesByScore.keys()) # may have gone up if self._trace > 0: print("Training Brill tagger on %d tokens..." % len(train_tokens)) # Maintain a list of the rules that apply at each position. rules_by_position = [{} for tok in train_tokens] # Create and return a tagger from the rules we found. return Brill(self._initial_tagger, rules)
#고정점이란 수열의 원소 중에서 그 값이 인덱스와 동일한 원소 #하나의 수열이 N개의 서로 다른 원소를 포함. 고정점 출력 #고정점이 나오고 나서, 그 다음 원소가 고정점이 아니라면 그 후 고정점은 없다 from bisect import bisect_right, bisect_left arr = list(map(int, input().split())) mid = (max(arr) + min(arr)) // 2 right_idx = bisect_right(arr, mid) left_idx = bisect_left(arr, mid) flag = False #고정점 발견하면 true while left_idx < right_idx: if (arr[left_idx] == left_idx): print(left_idx) #고정점 출력 flag = True if (flag == True and arr[left_idx] != left_idx): break #더 이상 고정점 없음 left_idx += 1 if (flag == False or (left_idx == right_idx and flag == True)): while right_idx < len(arr): if (arr[right_idx] == right_idx): print(right_idx) flag = True if (flag == True and arr[right_idx] != right_idx): break right_idx += 1 if (flag == False): print(-1)
def BinarySearch(a, x): i = bisect_left(a, x) if i != len(a) and a[i] == x: return i else: return -1
import numpy as np import bisect n, k = map(int, input().split()) a = np.array(list(map(int, input().split()))) s = np.cumsum(a) res = 0 for i in a: b = bisect.bisect_left(s, k) k = k + i res += n - b print(res)
def bisect_in(a, x): i = bisect.bisect_left(a, x) return i != len(a) and a[i] == x
def update_conformance(destination, existing_repo, patches_dir): print("Trying to import WebGL tests into {}".format(destination)) if existing_repo: directory = existing_repo print("Using existing WebGL repository: {}".format(directory)) else: directory = tempfile.mkdtemp() print("Cloning WebGL repository into temporary directory {}".format(directory)) subprocess.check_call(["git", "clone", KHRONOS_REPO_URL, directory, "--depth", "1"]) suite_dir = os.path.join(directory, "sdk/tests") print("Test suite directory: {}".format(suite_dir)) if not os.path.isdir(suite_dir): print("Test suite directory ({}) not found, aborting...".format(suite_dir)) sys.exit(1) # We recursively copy all the test suite to `destination` shutil.copytree(suite_dir, destination) # Get all the tests, remove any html file which is not in the list, and # later process them. tests = [] get_tests(destination, "00_test_list.txt", tests) test_count = len(tests) print("Found {} tests.".format(test_count)) print("Removing non-test html files") # To use binary search, which speeds things up a little # instead of f in tests tests.sort() # Remove html files that are not tests for dirpath, dirnames, filenames in os.walk(destination): if '/resources' in dirpath: continue # Most of the files under resources directories are used for f in filenames: if not f.endswith('.html'): continue f = os.path.join(dirpath, f) pos = bisect.bisect_left(tests, f) if pos == test_count or tests[pos] != f: print("Removing: {}".format(f)) os.remove(f) # Insert our harness into the tests for test in tests: process_test(test) # Try to apply the patches to the required files if not patches_dir: patches_dir = os.path.abspath(os.path.dirname(sys.argv[0])) for patch, file_name in PATCHES: try: patch = os.path.join(patches_dir, patch) if file_name is None: subprocess.check_call(["patch", "-d", destination, "-p", "1"], stdin=open(patch)) else: subprocess.check_call(["patch", "-x", "3", "-d", destination, file_name, patch]) except subprocess.CalledProcessError: print("Automatic patch failed for {}".format(file_name)) print("Please review the WPT integration and update {} accordingly".format(os.path.basename(patch)))