Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/idna/intranges.py
7790 views
"""1Given a list of integers, made up of (hopefully) a small number of long runs2of consecutive integers, compute a representation of the form3((start1, end1), (start2, end2) ...). Then answer the question "was x present4in the original list?" in time O(log(# runs)).5"""67import bisect8from typing import List, Tuple910def intranges_from_list(list_: List[int]) -> Tuple[int, ...]:11"""Represent a list of integers as a sequence of ranges:12((start_0, end_0), (start_1, end_1), ...), such that the original13integers are exactly those x such that start_i <= x < end_i for some i.1415Ranges are encoded as single integers (start << 32 | end), not as tuples.16"""1718sorted_list = sorted(list_)19ranges = []20last_write = -121for i in range(len(sorted_list)):22if i+1 < len(sorted_list):23if sorted_list[i] == sorted_list[i+1]-1:24continue25current_range = sorted_list[last_write+1:i+1]26ranges.append(_encode_range(current_range[0], current_range[-1] + 1))27last_write = i2829return tuple(ranges)3031def _encode_range(start: int, end: int) -> int:32return (start << 32) | end3334def _decode_range(r: int) -> Tuple[int, int]:35return (r >> 32), (r & ((1 << 32) - 1))363738def intranges_contain(int_: int, ranges: Tuple[int, ...]) -> bool:39"""Determine if `int_` falls into one of the ranges in `ranges`."""40tuple_ = _encode_range(int_, 0)41pos = bisect.bisect_left(ranges, tuple_)42# we could be immediately ahead of a tuple (start, end)43# with start < int_ <= end44if pos > 0:45left, right = _decode_range(ranges[pos-1])46if left <= int_ < right:47return True48# or we could be immediately behind a tuple (int_, end)49if pos < len(ranges):50left, _ = _decode_range(ranges[pos])51if left == int_:52return True53return False545556