def find_closest_elements_in_sorted_arrays(sorted_arrays):
    # TODO - you fill in here.

    min_diff = float('inf')

    chosen_set = []
    iters = SortedDict()

    for idx, s in enumerate(sorted_arrays):
        it = iter(s)
        first_min = next(it, None)
        assert first_min is not None, pdb.set_trace()
        if first_min is not None:
            iters[first_min] = (idx, it)

    while True:
        max_val = iters.keys()[-1]
        min_val, (min_idx, it) = iters.peekitem(0)

        if min_diff > max_val - min_val:
            min_diff = max_val - min_val
            chosen_set = sorted(list((v, i) for v, (i, _) in iters.items()),
                                key=lambda x: x[1])
        iters.popitem(0)
        next_min = next(it, None)
        if next_min == None:
            print(chosen_set)
            return min_diff
        iters[next_min] = (min_idx, it)
        assert len(iters) == len(sorted_arrays), pdb.set_trace()
Пример #2
0
def compute(N, K):

    lst = SortedDict()

    def lst_add(length, mult):
        if length in lst:
            oldval = lst[length]
            lst[length] = oldval + mult
        else:
            lst[length] = mult

    def lst_rem(length, mult=-1):
        if mult == -1:
            mult = lst[length]

        if length in lst:
            if lst[length] == mult:
                del lst[length]
            else:
                oldval = lst[length]
                lst[length] = oldval - mult
        else:
            raise ValueError

    lst[N] = 1

    i = 1
    while i < K:
        left = K - i

        t = lst.popitem()
        lent = t[0]  # length
        mult = t[1]
        use = min(left, mult)

        new_length1 = int(floor((lent - 1) / 2))
        new_length2 = int(ceil((lent - 1) / 2))
        if new_length1 > 0:
            lst_add(new_length1, use)
        if new_length2 > 0:
            lst_add(new_length2, use)
        if use < mult:
            lst_add(lent, mult - use)

        i += use

    t = lst.popitem()
    tl = t[0]
    new_length1 = int(floor((tl - 1) / 2))
    new_length2 = int(ceil((tl - 1) / 2))

    return (max(new_length1, new_length2), min(new_length1, new_length2))
Пример #3
0
class SortedSlidingDic:

    def __init__(self, stime):
        self.flow_dic = {}
        self.ts_dic = SortedDict()

        self.stime = float(self.unified_ts(stime))

    def unified_ts(self, ts):
        return round(ts, 10)

    def update(self, ts):
        ts = self.unified_ts(ts)

        while len(self.ts_dic) > 0 and ts - self.ts_dic.peekitem(0)[0] > self.stime:
            del self.flow_dic[self.ts_dic.peekitem(0)[1]]
            self.ts_dic.popitem(0)

    def add(self, flow, ts):
        ts = self.unified_ts(ts)

        # Remove the previous timestamp for this flow and the new one instead
        self.remove(flow)

        self.flow_dic[flow] = ts

        if ts in self.ts_dic:
            del self.flow_dic[self.ts_dic[ts]]

        self.ts_dic[ts] = flow

        assert len(self.flow_dic) == len(self.ts_dic)

    def remove(self, flow):
        if flow in self.flow_dic:
            ts = self.flow_dic[flow]

            try:
                del self.flow_dic[flow]
                self.ts_dic.pop(ts)
            except KeyError:
                print 'KeyError ', flow, ts

    def __str__(self):
        res = ''
        for k in self.ts_dic:
             res += str("%.10f" % k)+'\t'+str(self.ts_dic[k])+'\n'

        print res
Пример #4
0
def stalls(n_stalls, n_people):
    # depth = int(math.log(n_people, 2))

    stall_counts = SortedDict()

    add_count(stall_counts, n_stalls, 1)

    n_p = n_people

    while n_p > 0:

        if len(stall_counts) <= 0:
            error("0")
            return 0, 0

        # print("{}\t{}".format(n_p, stall_counts))

        n_s, count = stall_counts.popitem()

        n_p -= count

        min_d = (n_s - 1) // 2
        max_d = (n_s - 1) - min_d

        if n_p <= 0:
            # if n_p < 0:
            #     error(n_p)
            return max_d, min_d
        add_count(stall_counts, min_d, count)
        add_count(stall_counts, max_d, count)
Пример #5
0
def to_lyndon_basis(
        words,  # Linear[word]
):
    assert isinstance(words, Linear)
    terms_to_convert = SortedDict(words.items())
    terms_converted = Linear()
    while terms_to_convert:
        word_orig, coeff = terms_to_convert.popitem(-1)
        lyndon_words = lyndon_factorize(word_orig)
        assert len(lyndon_words) > 0
        lyndon_word_expr = Linear.count(lyndon_words)
        if len(lyndon_words) == 1:
            terms_converted[word_orig] += coeff
            continue
        denominator = 1
        for _, count in lyndon_word_expr.items():
            denominator *= math.factorial(count)
        expanded_word_expr = Linear.count(
            shuffle_product(lyndon_words)).div_int(denominator)
        assert expanded_word_expr[
            word_orig] == 1, f"{word_orig} not in {expanded_word_expr}"
        # print(f"Lyndon transform: {word_orig} => {lyndon_words} =>\n{expanded_word_expr}")
        expanded_word_expr[word_orig] = 0
        for word, inner_coeff in expanded_word_expr.items():
            assert terms_converted[word] == 0
            if not word in terms_to_convert:
                terms_to_convert[word] = 0
            terms_to_convert[word] -= coeff * inner_coeff
    return terms_converted
Пример #6
0
class SDWaitQueue(Generic[K, V]):
    __slots__ = ('_data', )

    def __init__(self):
        self._data = SortedDict()  # type: SortedDict[K, deque[V]]

    def __bool__(self):
        return bool(self._data)

    def __len__(self):
        return sum(len(item) for item in self._data.values())

    def push(self, key: K, item: V):
        try:
            self._data[key].append(item)
        except KeyError:
            self._data[key] = elements = deque()  # type: deque[V]
            elements.append(item)

    def pop(self) -> 'Tuple[K, deque[V]]':
        return self._data.popitem(0)

    def __repr__(self):
        return '<SDWaitQueue [%s]>' % ''.join(
            '%s: %s' % (key, value) for key, value in self._data.items())
Пример #7
0
def runsimulation(t_max, males, F_per_M, females,female_visit_param, dist, bird_speed, improb_sds,improb_dist,FG_tau_mean, FG_tau_std,FG_tau_range, FG_tau_norm_range,FG_k, FG_theta, FG_divisor,RBSB_tau_mean, RBSB_tau_std, RBSB_tau_norm_range, damage_to_bower, male_pos, male_strat):
    global birds
    global timeline
    global female_birds
    timeline = SortedDict()
    # BIRDS
    birds = []
    # initialize positions, travel times and preferences
    distances, travel_times = compute_distances_travel_times(males, male_pos, bird_speed, dist)
    visit_preferences = compute_visit_preferences(males, distances, improb_dist, improb_sds)
    for i in range(males):
        birds.append(initialize_male(i, 
                                     male_strat[i], 
                                     male_pos[i], 
                                     visit_preferences[i],
                                     travel_times[i]))
        # choose its first action
        choose_action(birds[-1], 0.0)
    #initialize females
    female_birds=[]
    for i in range(females): #females
        female_id = "F" + str(i)
        female_birds.append(initialize_female(female_id, males)) #female IDs start where males end (if there are 10 males, the first female would be 11)
        #choose time for initial mating attempt
        first_time =  female_visit_param[0] * truncnorm.rvs(female_visit_param[2], female_visit_param[3]) + female_visit_param[1] 
        action_mating_attempt(female_id, first_time)

    # this is the main loop (for one mating season)
    while len(timeline) > 0:
        current_ticket = timeline.popitem(0)
        read_ticket(current_ticket[1])
    return birds
def find_closest_elements_in_sorted_arrays(sorted_arrays):
    # TODO - you fill in here.

    min_diff = float('inf')

    iters = SortedDict()
    chosen_set = None

    for idx, s in enumerate(sorted_arrays):
        it = iter(s)
        first_min = next(it, None)
        if first_min is not None:
            iters[(first_min, idx)] = it

    while True:
        max_val = iters.keys()[-1][0]
        min_val, min_idx = iters.keys()[0]

        if min_diff > max_val - min_val:
            min_diff = max_val - min_val
            chosen_set = sorted(list(iters.keys()), key=lambda x: x[1])
        it = iters.popitem(0)[1]

        next_min = next(it, None)
        if next_min == None:
            print(chosen_set)
            return min_diff
        iters[(next_min, min_idx)] = it
        assert len(iters) == len(sorted_arrays), pdb.set_trace()
Пример #9
0
class SortedDictBook:
    def __init__(self, depth):
        self.depth = depth
        self.orders = SortedDict()
        self.empty_price = PriceUpdate()

    def update(self, price_update: PriceUpdate) -> bool:
        action = {
            48: self.new_order,
            49: self.update_order,
            50: self.delete_order,
            51: self.delete_thru,
            52: self.delete_from
        }.get(price_update.action, None)

        if action:
            action(price_update)
            return True

        return False

    def update_order(self, price_update: PriceUpdate):
        self.orders[price_update.price] = price_update

    def delete_order(self, price_update: PriceUpdate):
        self.orders.pop(price_update.price)

    def delete_thru(self, price_update: PriceUpdate):
        self.orders.clear()

    def new_order(self, price_update: PriceUpdate):
        if len(self.orders) == self.depth:
            self.orders.popitem()

        self.orders[price_update.price] = price_update

    def get_book(self) -> List[PriceUpdate]:
        return self.orders.values()

    def delete_from(self, price_update: PriceUpdate):
        direction = price_update.level
        del self.orders.iloc[:direction]

    def top(self) -> PriceUpdate:
        return self.orders.peekitem(0)[1] if self.orders else self.empty_price
def k_most_visited(stream, k):
    sorted_dict = SortedDict()
    for page in stream:
        if page not in sorted_dict:
            sorted_dict[page] = 1
        else:
            sorted_dict[page] += 1

    return [sorted_dict.popitem(index=0) for x in range(k)]
class RangeModule:

    def __init__(self):
        self.data = SortedDict()

    def addRange(self, left: int, right: int) -> None:
        l, r = self.data.bisect(left), self.data.bisect(right)
        if l != 0:
            # move L to the left by 1 this will point to the lower bound
            l -= 1
            # if the left is larger than the previous interval we need to move it up
            if self.data.peekitem(l)[1] < left:
                l += 1
        if l != r:
            # given the adjust left and right intervals we need to check if a merge needs to happen. we take
            # the min of the left intervals and the max of the right intervals to maximize the interval size.
            left, right = min(left, self.data.peekitem(l)[0]), max(right, self.data.peekitem(r-1)[1])
            # now that we have the new interval we need ot pop the redundant intervals
            for _ in range(l, r):
                self.data.popitem(l)
        # insert the new interval
        self.data[left] = right
        print(self.data)

    def queryRange(self, left: int, right: int) -> bool:
        l, r = self.data.bisect_right(left), self.data.bisect_right(right)
        print("l == 0: ",(l == 0), "self.data.peekitem(l-1)[1] < right: ",(self.data.peekitem(l-1)[1] < right))
        if l == 0 or self.data.peekitem(l-1)[1] < right: return False

        return True

    def removeRange(self, left: int, right: int) -> None:
        l, r = self.data.bisect_right(left), self.data.bisect_right(right)
        if l != 0:
            l -= 1
            if self.data.peekitem(l)[1] < left:
                l += 1
        if l != r:
            ll, rr = min(left, self.data.peekitem(l)[0]), max(right, self.data.peekitem(r-1)[1])
            for _ in range(l, r):
                self.data.popitem(l)
            if ll < left: self.data[ll] = left
            if right < rr: self.data[right] = rr
Пример #12
0
class LFUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.capUsage = 0
        self.keyValue = {}
        self.keyCnt = {}
        self.cntKey = SortedDict()

    def get(self, key: int) -> int:
        # print(f"get {key} ")
        # print("keyCnt ",self.keyCnt)
        # print("cnt ", self.cntKey)

        if self.capacity == 0 or key not in self.keyValue: return -1

        ret = self.keyValue[key]
        ntimes = self.keyCnt[key]
        self.cntKey[ntimes + 1] = self.cntKey.get(ntimes + 1, OrderedDict())
        self.cntKey[ntimes + 1][key] = 1

        self.keyCnt[key] += 1

        del self.cntKey[ntimes][key]

        if len(self.cntKey[ntimes]) == 0:
            del self.cntKey[ntimes]
        # print("keyCnt: ", self.keyCnt)
        # print("cntKey: ", self.cntKey)
        return ret

    def put(self, key: int, value: int) -> None:
        # print(f"put {key} ")
        # print("keyCnt: ", self.keyCnt)
        # print("cnt: ", self.cntKey)

        if self.capacity == 0: return None
        if key in self.keyValue:
            self.get(key)
            self.keyValue[key] = value
        ## delete least freq used
        else:
            if self.capUsage == self.capacity:
                ntimes, ndict = self.cntKey.popitem(0)
                self.capUsage -= 1
                oldKey, _ = ndict.popitem(last=False)
                del self.keyCnt[oldKey]
                del self.keyValue[oldKey]
                if ndict:
                    self.cntKey[ntimes] = ndict
            self.capUsage += 1
            self.keyValue[key] = value
            self.keyCnt[key] = 1

            self.cntKey[1] = self.cntKey.get(1, OrderedDict())
            self.cntKey[1][key] = 1
Пример #13
0
class RangeModuleDict:
    def __init__(self):
        self.data = SortedDict()

    def addRange(self, left: int, right: int) -> None:
        l = self.data.bisect(left)
        r = self.data.bisect(right)
        if l != 0:
            l -= 1
            if self.data.peekitem(l)[1] < left:
                l += 1
        if l != r:
            left = min(left, self.data.peekitem(l)[0])
            right = max(right, self.data.peekitem(r - 1)[1])
            for _ in range(l, r):
                self.data.popitem(l)
        self.data[left] = right

    def queryRange(self, left: int, right: int) -> bool:
        l = self.data.bisect_right(left)
        r = self.data.bisect_right(right)
        if l == 0 or self.data.peekitem(l - 1)[1] < right:
            return False
        return True

    def removeRange(self, left: int, right: int) -> None:
        l = self.data.bisect_right(left)
        r = self.data.bisect_right(right)
        if l != 0:
            l -= 1
            if self.data.peekitem(l)[1] < left:
                l += 1
        if l != r:
            minLeft = min(left, self.data.peekitem(l)[0])
            maxRight = max(right, self.data.peekitem(r - 1)[1])
            for _ in range(l, r):
                self.data.popitem(l)
            if minLeft < left:
                self.data[minLeft] = left
            if right < maxRight:
                self.data[right] = maxRight
Пример #14
0
class EventsA:
    def __init__(self):
        self.events = SortedDict()

    def add_event(self, event):
        self.events[event.frame] = event

    def delete_event(self, index):
        self.events.popitem(index)

    def render(self, offsets, timecode):
        """
        Return data suitable for display in LogTable
        :param offsets: Offsets object which contains frame offsets used to generate timecodes that match video
        :param timecode: Timecode object (with predefined framerate, drop_frame) used to generate timecode strings
        """
        data = []
        for event in self.events:
            timecode.frames = event.frame + 1 + offsets.get_offset(event.frame)
            data.append([event.trial, event.status, event.response, str(timecode)])
        return data
Пример #15
0
def arrayRDP(arr, epsilon=0.0, n=None):
    """
    This is a slightly modified version of the _aRDP function, that accepts
    as arguments the tolerance in the distance and the maximum number of points
    the algorithm can select.
    **Note:** The results of this algoritm should be identical to the arrayRDP
    function if the *n* parameter is not specified. In that case, the
    performance is slightly worse, although the asymptotic complexity is the
    same. For this reason, this function internally delegates the solution in
    that function if the *n* parameter is missing.

    Parameters
    ----------
    arr:
        Array of values of consecutive points.
    epsilon:
        Maximum difference allowed in the simplification process.
    n:
        Maximum number of points of the resulted simplificated array.

    Returns
    -------
    out:
        Array of indices of the selected points.
    """
    if n is None:
        return _aRDP(arr, epsilon)
    if epsilon <= 0.0:
        raise ValueError('Epsilon must be > 0.0')
    n = n or len(arr)
    if n < 3:
        return arr
    fragments = SortedDict()
    #We store the distances as negative values due to the default order of
    #sorteddict
    dist, idx = max_vdist(arr, 0, len(arr) - 1)
    fragments[(-dist, idx)] = (0, len(arr) - 1)
    while len(fragments) < n-1:
        (dist, idx), (first, last) = fragments.popitem(last=False)
        if -dist <= epsilon:
            #We have to put again the last item to prevent loss
            fragments[(dist, idx)] = (first, last)
            break
        else:
            #We have to break the fragment in the selected index
            dist, newidx = max_vdist(arr, first, idx)
            fragments[(-dist, newidx)] = (first, idx)
            dist, newidx = max_vdist(arr, idx, last)
            fragments[(-dist, newidx)] = (idx, last)
    #Now we have to get all the indices in the keys of the fragments in order.
    result = SortedList(i[0] for i in fragments.itervalues())
    result.add(len(arr) - 1)
    return np.array(result)
def compare_faces(list_of_face_encodings, unknown_face_encodings):
    """
    Function which find 4 person which closest.
    """

    result_list_of_faces = SortedDict()

    for element in list_of_face_encodings:
        # calculate distance from current encodings to unknown encodings
        current_distance = face_recognition.api.face_distance(
            element["face_encoding"], unknown_face_encodings)
        min_dist = current_distance.min()
        # add to our list of top matches faces
        if len(result_list_of_faces) < 3:
            result_list_of_faces.update({min_dist: element})
        else:
            result_list_of_faces.update({min_dist: element})
            result_list_of_faces.popitem()
    if result_list_of_faces.keys()[0] > 0.6:
        result_list_of_faces = None
    return result_list_of_faces
Пример #17
0
def top(partition_iterator, N):
    # create a SortedDict object, but keep the size to N
    sd = SortedDict()
    #
    for key, number in partition_iterator:
        # add an entry of (key, number) at sd
        sd[number] = key
        # always keep only Top-N items
        if (len(sd) > N):
            # remove the smallest number
            sd.popitem(0)
            ### NOTE:
            ###    to find bottom-N, replace
            ###    "sd.popitem(0)" with "sd.popitem(-1)"
    #end-for
    #print("local sd=", sd)
    #
    # switch key with value
    pairs = [(v, k) for k, v in sd.items()]
    #print("local top N pairs=", pairs)
    return pairs
Пример #18
0
def generate_first_k_a_b_sqrt2(k):
    nums = SortedDict()
    nums[0.0] = Number(0, 0)
    result = []
    while len(result) < k:
        nxt = nums.popitem(0)[1]
        result.append(nxt.val)
        a = Number(nxt.a + 1, nxt.b)
        b = Number(nxt.a, nxt.b + 1)
        nums[a.val] = a
        nums[b.val] = b
    return result
Пример #19
0
def ans(n, k):
    m = SortedDict()
    m[n] = 1
    bg = None
    while k > 0:
        bg, slots = m.popitem()
        k -= slots
        x, y = divide(bg)
        m[x] = m[x]+slots if x in m else slots
        m[y] = m[y]+slots if y in m else slots

    return ' '.join([str(x) for x in reversed(sorted(split(bg)))])
Пример #20
0
def arrayRDP(arr, epsilon=0.0, n=None):
    """
    This is a slightly modified version of the _aRDP function, that accepts
    as arguments the tolerance in the distance and the maximum number of points
    the algorithm can select.
    **Note:** The results of this algoritm should be identical to the arrayRDP
    function if the *n* parameter is not specified. In that case, the
    performance is slightly worse, although the asymptotic complexity is the
    same. For this reason, this function internally delegates the solution in
    that function if the *n* parameter is missing.

    Parameters
    ----------
    arr:
        Array of values of consecutive points.
    epsilon:
        Maximum difference allowed in the simplification process.
    n:
        Maximum number of points of the resulted simplificated array.

    Returns
    -------
    out:
        Array of indices of the selected points.
    """
    if n is None:
        return _aRDP(arr, epsilon)
    if epsilon <= 0.0:
        raise ValueError('Epsilon must be > 0.0')
    n = n or len(arr)
    if n < 3:
        return arr
    fragments = SortedDict()
    #We store the distances as negative values due to the default order of
    #sorteddict
    dist, idx = max_vdist(arr, 0, len(arr) - 1)
    fragments[(-dist, idx)] = (0, len(arr) - 1)
    while len(fragments) < n - 1:
        (dist, idx), (first, last) = fragments.popitem(last=False)
        if -dist <= epsilon:
            #We have to put again the last item to prevent loss
            fragments[(dist, idx)] = (first, last)
            break
        else:
            #We have to break the fragment in the selected index
            dist, newidx = max_vdist(arr, first, idx)
            fragments[(-dist, newidx)] = (first, idx)
            dist, newidx = max_vdist(arr, idx, last)
            fragments[(-dist, newidx)] = (idx, last)
    #Now we have to get all the indices in the keys of the fragments in order.
    result = SortedList(i[0] for i in fragments.itervalues())
    result.add(len(arr) - 1)
    return np.array(result)
Пример #21
0
def solve(n, k):
    # Initial state, we have one "space" of size n
    m = SortedDict()
    m[n] = 1
    bg = None
    while k > 0:
        bg, slots = m.popitem()
        k -= slots
        x, y = split(bg)
        m[x] = m[x] + slots if x in m else slots
        m[y] = m[y] + slots if y in m else slots

    return ' '.join([str(x) for x in reversed(sorted(split(bg)))])
Пример #22
0
    def lastStoneWeight(self, stones):
        stones_sorted_dict = SortedDict(Counter(stones))
        # print(stones_sorted_dict)
        while len(stones_sorted_dict) != 1:
            last = None

            while len(stones_sorted_dict) > 0:
                q = stones_sorted_dict.popitem()
                if q[1] % 2:
                    last = q[0]
                if last:
                    break
            if len(stones_sorted_dict) == 0:
                if last is None:
                    return 0
                else:
                    return last

            second_last = stones_sorted_dict.peekitem()[0]
            stones_sorted_dict[second_last] -= 1

            if stones_sorted_dict[second_last] == 0:
                stones_sorted_dict.popitem()

            q = last - second_last

            if q in stones_sorted_dict:
                stones_sorted_dict[q] += 1

            else:
                stones_sorted_dict[q] = 1

            # print(stones_sorted_dict)

        if len(stones_sorted_dict) == 1:
            if stones_sorted_dict.peekitem()[1] % 2:
                return stones_sorted_dict.peekitem()[0]
            else:
                return 0
Пример #23
0
def get_top_programs(logfile, n=None):
    sorted_programs = SortedDict()

    with open(logfile) as logf:
        for line in logf.readlines():
            match = re.fullmatch('.*NPE: ([0-9]+).*Tot R: ([\.\-0-9]+)\..*Program: ([><\^\+-\[\]\.,!01234abcde]*)\n', line)
            if match:
                idx, reward, program = match.groups()
                idx = int(idx)
                reward = float(reward)
                sorted_programs[reward] = (idx, program)

    if n:
        for i in range(n):
            if not sorted_programs:
                break

            reward, (idx, program) = sorted_programs.popitem()
            yield reward, idx, program
    else:
        while sorted_programs:
            reward, (idx, program) = sorted_programs.popitem()
            yield reward, idx, program
Пример #24
0
class BaseNode(object):
    def __init__(self, tree):
        self.tree = tree
        self.bucket = SortedDict()
        self.changed = False

    def _split(self):
        """
        Creates a new node of the same type and splits the contents of the
        bucket into two parts of an equal size. The lower keys are being stored
        in the bucket of the current node. The higher keys are being stored in
        the bucket of the new node. Afterwards, the new node is being returned.
        """
        other = self.__class__(tree=self.tree)
        size = len(self.bucket)
        for i in range(int(size/2)):
            key, value = self.bucket.popitem()
            other.bucket[key] = value

        # print("New node created: " + str(other))
        return LazyNode(node=other, tree=self.tree)

    def _insert(self, key, value):
        """
        Inserts the key and value into the bucket. If the bucket has become too
        large, the node will be split into two nodes.
        """

        self.bucket[key] = value
        self.changed = True
        # print(str(key)+" inserted into: " + str(self.bucket))
        if len(self.bucket) > self.tree.max_size:
            new_node = self._split()
            new_node.node.changed = True
            return new_node

        pass

    def _get_data(self):
        """
        Returns the encoded data of the leaf node, containing its type, and the
        key/value pairs. These values will eventually be the offsets of the
        documents.
        """

        # print("Leaf committed: " + str(self) + " bucketsize: " +
        #     str(len(self.bucket)))
        data = {"type":"Leaf", "entries":self.bucket}
        # print("Leaf data: "+ str(data))
        return(add_integrity(encode(data)))
Пример #25
0
class BaseSimulator:

    def __init__(self):
        self.currentTime = 0.0
        self.eventList = SortedDict()

    def addEvent(self, time, funcName, args):
        self.eventList[time] = [funcName, args]

    def deleteEvents(self, timeList):
        try:
            for key in timeList:
                self.eventList.pop(key)
        except KeyError:
            return

    def completeRun(self):
        try:
            while self.eventList.peekitem(0):
                self.currentTime, funcCall = self.eventList.peekitem(0)
                funcCall[0](funcCall[1])
                self.eventList.popitem(0)
        except IndexError:
            return

    def run(self, timeLimit = 0):
        try:
            while self.eventList.peekitem(0):
                currentTime, funcCall = self.eventList.peekitem(0)
                if 'timeLimit' in locals():
                    if currentTime >= timeLimit:
                        return
                funcCall[0](funcCall[1])
                self.currentTime = currentTime
                self.eventList.popitem(0)
        except IndexError:
            return
Пример #26
0
 def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
     dic = SortedDict()
     for idx, row in enumerate(mat):
         sm = sum(row)
         if sm in dic:
             dic[sm].append(idx)
         else:
             dic.setdefault(sm, [idx])
     ans = []
     k1 = k
     while k > 0:
         ls_curr = dic.popitem(0)[1]
         ans.extend(ls_curr)
         k -= len(ls_curr)
     return ans[:k1]
Пример #27
0
class Timeline:
    def __init__(self):
        self._time_cells = SortedDict()

    def add(self, event: Any, occurring: int):
        if occurring in self._time_cells:
            self._time_cells[occurring].append(event)
        else:
            self._time_cells[occurring] = [event]

    def __iter__(self):
        while self._time_cells:
            occurring, event_list = self._time_cells.popitem(0)
            for event in event_list:
                yield occurring, event
Пример #28
0
def solve(case_no):
    n, k = map(int, input().split())

    c = SortedDict()
    c[n] = 1
    k -= 1

    while k:
        t, cnt = c.popitem()
        if (t - 1) // 2 not in c:
            c[(t - 1) // 2] = 0
        if t // 2 not in c:
            c[t // 2] = 0
        c[(t - 1) // 2] += min(k, cnt)
        c[t // 2] += min(k, cnt)
        if cnt <= k:
            k -= cnt
        else:
            c[t] = cnt - k
            k = 0

    t = c.popitem()[0]
    mn, mx = (t - 1) // 2, t // 2
    print("Case #{}: {} {}".format(case_no, mx, mn))
Пример #29
0
def generate_first_k_a_b_sqrt2(k):
    # TODO - you fill in here.
    candidates = SortedDict()

    candidates[0] = (0, 0)

    result = []
    while candidates and len(result) < k:
        v, (a, b) = candidates.popitem(0)

        result.append(v)

        candidates[(a + 1) + b * 2**0.5] = a + 1, b
        candidates[a + (b + 1) * 2**0.5] = a, b + 1

    return result
def find_closest_elements_in_sorted_arrays(sorted_arrays):
    diff = float('inf')
    tree = SortedDict()
    for idx, array in enumerate(sorted_arrays):
        i = iter(array)
        nxt = next(i)
        tree[(nxt, idx)] = i
    while True:
        min_val, min_idx = tree.peekitem(0)[0]
        max_val = tree.peekitem()[0][0]
        diff = min(diff, max_val - min_val)
        i = tree.popitem(0)[1]
        nxt = next(i, None)
        if nxt is None:
            return diff
        tree[(nxt, min_idx)] = i
class Events:
    def __init__(self):
        self.list_of_events = SortedDict()  # for each time, a list of events

    def add_event(self, event):
        if not self.list_of_events.get(event.time):
            self.list_of_events[event.time] = [event]
        else:
            self.list_of_events[event.time].append(event)

    def count_arrivals_at(self, time):
        try:
            li = self.list_of_events[time]
            n = 0
            for e in li:
                if li.type == 'arrival':
                    n += 1
            return n
        except:
            return 0

    def pop_next_event(self):
        li = self.list_of_events.peekitem(0)[1]
        #if debug:
        #    print(self.list_of_events)
        if (len(li) > 1):
            toret = li.pop(0)
            #if debug:
            #    print(f'returning {toret}')
            return toret
        else:
            lil = self.list_of_events.popitem(0)[1]
            toret = lil[0]
            #if debug:
            #    print(f'returning {toret}')
            return toret

    def count(self):
        return len(self.list_of_events)

    def __str__(self):
        s = ''
        for k in self.list_of_events.keys():
            s += (str(k) + ": ")
            for e in self.list_of_events[k]:
                s += str(e) + ', '
        return s
Пример #32
0
def runsimulation(t_max, males, F_per_M, females, female_visit_param,
                  male_dist, bird_speed, FG_tau_mean, FG_tau_std, FG_tau_range,
                  FG_tau_norm_range, FG_k, FG_theta, FG_divisor, RBSB_tau_mean,
                  RBSB_tau_std, RBSB_tau_norm_range, damage_to_bower,
                  max_maraud, n_mar, mar_ids):
    global birds
    global timeline
    global female_birds
    timeline = SortedDict()
    # BIRDS
    birds = []
    female_birds = []
    mars = eval(mar_ids)
    strategies = np.zeros(males)
    strategies[mars] = max_maraud

    # initialize positions, travel times and preferences
    travel_times = compute_distances_travel_times(male_dist, bird_speed)
    for i in range(males):
        birds.append(
            initialize_male(
                i,
                strategies[
                    i],  #strategy for bird 0 is 0 (guarder) and 1 is max_maraud (marauder)
                travel_times))
        # choose its first action
        choose_action(birds[-1], 0.0, travel_times)

    #initialize females
    for i in range(females):  #females
        female_id = "F" + str(i)
        female_birds.append(
            initialize_female(female_id, males)
        )  #female IDs start where males end (if there are 10 males, the first female would be 11)
        #choose time for initial mating attempt
        first_time = female_visit_param[0] * truncnorm.rvs(
            female_visit_param[2],
            female_visit_param[3]) + female_visit_param[1]
        action_mating_attempt(female_id, first_time, travel_times)

    # this is the main loop
    while len(timeline) > 0:
        current_ticket = timeline.popitem(0)
        read_ticket(current_ticket[1], travel_times)

    return birds
Пример #33
0
class LayerDirector:

  verboseUnits = True

  def __init__(self, nClusters, nTotalUnits, Ti, Tn, NBin_nEntries, ZF):

    #schedule
    self.wakeQ = SortedDict()
    self.now = 0

    self.ZF = ZF
    self.VERBOSE = op.verboseDirector
    self.nClusters = nClusters
    self.Tn = Tn  # it is used when assigning filters to clusters
    self.nUnitsCluster = nTotalUnits / nClusters
 
    #components
    self.centralMem = simpleMemory.SimpleMemory(self, op.CM_size, op.CM_nPorts, op.CM_bytesCyclePort)
    self.clusters = []
    self.coordsWindow = {}
    self.clustersProcWindow = {} # [windowID] -> count of clusters processing this window
    self.filtersPending = {}
    self.clustersReadingWindow = {}
    self.output = []
    for i in range(nClusters):
      self.clusters.append(cluster.Cluster(self, i, self.nUnitsCluster, Ti, Tn, NBin_nEntries, op.SB_size_per_cluster, self.cbClusterDoneReading, self.cbClusterDone))


  def schedule(self, entity, when = 1):
    when += self.now
    if when in self.wakeQ:
      self.wakeQ[when].add(entity)
    else:
      self.wakeQ[when] = set([entity])


  def wakeup(self):
    if self.VERBOSE > 1: print "layerdirector, cycle ", self.now, len(self.wakeQ)
    entities = [] 
    if len(self.wakeQ) !=0:
      aux, entities = self.wakeQ.popitem(False)
      if self.VERBOSE > 1: print "layerdirector, cycle ", self.now, aux, len(entities), " objects to wakeup"
      self.now = aux
      for obj in entities:
        obj.wakeup()


##################################################################################
###
### This function copy the filter weights into the unit eDRAM
###
### weights: ndarray containing the weights of the filters  
###
##################################################################################
  def initializeLayer(self, weights):
    self.nTotalFilters = weights.shape[0]
    #how many filters have to go to each cluster
    nFiltersPerCluster = self.nTotalFilters / self.nClusters
    if self.VERBOSE: print "%d filters per cluster" % (nFiltersPerCluster)
    # if the total number of filters is not a multiple of nClusters
    nAdditionalFilters = self.nTotalFilters - (nFiltersPerCluster * self.nClusters)
    if self.VERBOSE: print "plus %d additional filters"%(nAdditionalFilters)
   
    # send the units the size of the filters so they can configure SB properly (simulation) 
    for i in range(self.nClusters): 
      self.clusters[i].initialize(weights[0].size)

    ##for idxFilter in range(nTotalFilters):
      ##self.clusters[(idxFilter / self.nClusters) % self.nClusters].fill_SB(weights[idxFilter], idxFilter)
    
    filtersAssignedSoFar = 0
    idxFilter = 0
    cntCluster = 0

    while filtersAssignedSoFar < nFiltersPerCluster:
      cntFilterCluster = 0
      listFilterData = []
      listFilterIdxs = []
      while cntFilterCluster < min(self.Tn, nFiltersPerCluster-filtersAssignedSoFar ): 
        listFilterData.append(weights[idxFilter])
        listFilterIdxs.append(idxFilter)
        cntFilterCluster += 1
        idxFilter += 1
      self.clusters[cntCluster].fill_SB(listFilterData, listFilterIdxs)
      cntCluster += 1
      if cntCluster == self.nClusters:
        filtersAssignedSoFar += cntFilterCluster
        cntCluster = 0
      #print '%d %d %d %d %d'%(nFiltersPerCluster, filtersAssignedSoFar, cntFilterCluster, self.Tn, self.nClusters)

##################################################################################
# in:
# 	data : numpy 3D ndarray of dimensions i * Wx * Wy, i=# input features, Wx=Wy= size of input
#	filters: a list with two elements, we will use the field "data" of both, 
#		filters[0].data = numpy 4D ndarray  of dimensions N * i * Fx * Fy with the filter values
#		filters[1].data = numpy 1D vector with the N biases 
#		N = # filters, Fx=Fy= filter size
##################################################################################

  def computeConvolutionalLayer(self, data, filters, stride, padding, group):
    # TODO: when data come from modelzoo
    #weights = filters[0].data
    #biases  = filters[1].data
    weights = filters


    N	  = weights.shape[0]
    i	  = weights.shape[1]
    Fx	  = weights.shape[2]
    Fy	  = weights.shape[3]
  
    Ix 	  = data.shape[1]
    Iy	  = data.shape[2]
  
  
    if padding != 0:
      data = adjustDataPadding(data, padding)
      Ix      = data.shape[1]
      Iy      = data.shape[2]
  
  
    assert weights.shape[1]*group==data.shape[0], "#filters (%d) is not equal to #input features (%d)" %(weights.shape[1], data.shape[0])  
    assert Ix==Iy, "Input width (%d) is not equal to Input height (%d)" %(data.shape[1], data.shape[2]) 
    assert Fx==Fy, "Filter width (%d) is not equal to Filter height (%d)" %(Fx, Fy)
  
     # initialize the layer (filters are sent to SB in the units)
    self.initializeLayer(weights)
 
    #### Main loop ###
    # Horizontal shifting for window generation  
    self.output = np.zeros((N, (Ix-Fx)/stride+1, (Iy-Fy)/stride+1))
    windowID = 0
    outPosX = 0
    for posInputX in range(0, Ix-Fx+1, stride):
      outPosY = 0
      #print posInputX
      for posInputY in range(0, Iy-Fy+1, stride):
      # process each window
        auxWindow  = data[:, posInputY:posInputY+Fy, posInputX:posInputX+Fx]
        self.filtersPending[windowID] = [True] * self.nTotalFilters
 
        self.initializeWindow(auxWindow, windowID)
        self.startWindowProcessing(auxWindow, windowID)
        self.coordsWindow[windowID] = [outPosX, outPosY]
 
        timeout=10000
        while not self.isFinished(windowID) and timeout > 0:
          self.wakeup()
          timeout -= 1
        assert timeout, "Simulation Timed Out"
          
          #output[cntFilter, outPosY, outPosX] += biases[cntFilter]
        windowID += 1
        outPosY += 1
      outPosX += 1
    print "Total cycles: ", self.now 
  

  def isFinished(self, windowID):
    #print "Pending Count = ", np.sum(self.filtersPending[windowID])
    for i,e in enumerate(self.filtersPending[windowID]):
      if e:
        if self.VERBOSE > 1: 
          print "pendingFilters for window %d"%(windowID)
          a = []
          for j,e in enumerate(self.filtersPending[windowID]):
            if e: a.append(j)
          print a
        return False
    if self.VERBOSE > 1: print "pendingFilters for window %d  LISTO"%(windowID)
    return True
    

##################################################################################
###
##################################################################################
  def initializeWindow(self, windowData, windowID):
    if self.VERBOSE: print "[director] [%d] Initializing window #%d"%(self.now, windowID)

    #self.windowsDataFlat[windowID] = np.swapaxes(windowData, 0, 2).flatten()
    self.clustersProcWindow[windowID] = self.nClusters
    self.clustersReadingWindow[windowID] = self.nClusters 
    for cntCluster in range(self.nClusters):
      self.clusters[cntCluster].initializeWindow(windowData, windowID)

##################################################################################
###
##################################################################################

  def startWindowProcessing(self, windowData, windowID):
    if self.VERBOSE: print "[director] Processing of window #%d"%(windowID)

    for cntCluster in range(self.nClusters):
      self.schedule(self.clusters[cntCluster])

##################################################################################
###
##################################################################################
  def cbClusterDoneReading(self, clusterID, windowID):
    self.clustersReadingWindow[windowID] -= 1 

  def cbClusterDone(self, clusterID, windowID):
    self.clustersProcWindow[windowID] -= 1 


  def putData(self, windowID, data, filterIDs):
    if self.VERBOSE > 1: print "windowID: ", windowID, " filterIDs:", filterIDs 
    for f in filterIDs:
      self.filtersPending[windowID][f] = False

    x, y = self.coordsWindow[windowID]
    self.output[filterIDs, x, y] = data  
Пример #34
0
sys.stdout.flush() 

start = time.clock()

while len(upwords) < l - 2:
    for i in range(0,l):
        if i in upwords:
            continue
        for j in range(i+1,l):
            if j in upwords:
                continue

            dist = spatial.distance.cosine(a[i],a[j])
            tempp[dist] = (i,j)
            if len(tempp) > limit:
                tempp.popitem()
    dicl = len(tempp)
    for i in range(0,dicl):
        tmp = tempp.popitem(last=False)
        if tmp[1][0] not in upwords and tmp[1][1] not in upwords:
            l1.append(tmp[1])
            upwords.add(tmp[1][0])
            upwords.add(tmp[1][1])
    print 'l1len:',len(l1)
    sys.stdout.flush() 

l = len(l1)
upwords = set()

while len(upwords) < l - 2:
    for i in range(0,l):
def test_popitem2():
    temp = SortedDict()
    temp.popitem()
def test_popitem():
    mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
    temp = SortedDict(mapping)
    assert temp.popitem() == ('z', 25)
def test_popitem2():
    temp = SortedDict()
    with pytest.raises(KeyError):
        temp.popitem()
Пример #38
0
#   http://www.grantjenks.com/docs/sortedcontainers/
from sortedcontainers import SortedList
sl = SortedList(['e', 'a', 'c', 'd', 'b'])
print(sl)
sl *= 10000000
print(sl.count('c'))
print(sl[-3:])
['e', 'e', 'e']

from sortedcontainers import SortedDict
sd = SortedDict({'c': 3, 'a': 1, 'b': 2})
print(sd)
SortedDict({'a': 1, 'b': 2, 'c': 3})
print(sd.popitem())

from sortedcontainers import SortedSet
ss = SortedSet('abracadabra')
print(ss)
print(ss.bisect_left('c'))
Пример #39
0
class StreamChangeCache(object):
    """Keeps track of the stream positions of the latest change in a set of entities.

    Typically the entity will be a room or user id.

    Given a list of entities and a stream position, it will give a subset of
    entities that may have changed since that position. If position key is too
    old then the cache will simply return all given entities.
    """

    def __init__(self, name, current_stream_pos, max_size=10000, prefilled_cache=None):
        self._max_size = int(max_size * caches.CACHE_SIZE_FACTOR)
        self._entity_to_key = {}
        self._cache = SortedDict()
        self._earliest_known_stream_pos = current_stream_pos
        self.name = name
        self.metrics = caches.register_cache("cache", self.name, self._cache)

        if prefilled_cache:
            for entity, stream_pos in prefilled_cache.items():
                self.entity_has_changed(entity, stream_pos)

    def has_entity_changed(self, entity, stream_pos):
        """Returns True if the entity may have been updated since stream_pos
        """
        assert type(stream_pos) in integer_types

        if stream_pos < self._earliest_known_stream_pos:
            self.metrics.inc_misses()
            return True

        latest_entity_change_pos = self._entity_to_key.get(entity, None)
        if latest_entity_change_pos is None:
            self.metrics.inc_hits()
            return False

        if stream_pos < latest_entity_change_pos:
            self.metrics.inc_misses()
            return True

        self.metrics.inc_hits()
        return False

    def get_entities_changed(self, entities, stream_pos):
        """
        Returns subset of entities that have had new things since the given
        position.  Entities unknown to the cache will be returned.  If the
        position is too old it will just return the given list.
        """
        assert type(stream_pos) is int

        if stream_pos >= self._earliest_known_stream_pos:
            changed_entities = {
                self._cache[k] for k in self._cache.islice(
                    start=self._cache.bisect_right(stream_pos),
                )
            }

            result = changed_entities.intersection(entities)

            self.metrics.inc_hits()
        else:
            result = set(entities)
            self.metrics.inc_misses()

        return result

    def has_any_entity_changed(self, stream_pos):
        """Returns if any entity has changed
        """
        assert type(stream_pos) is int

        if not self._cache:
            # If we have no cache, nothing can have changed.
            return False

        if stream_pos >= self._earliest_known_stream_pos:
            self.metrics.inc_hits()
            return self._cache.bisect_right(stream_pos) < len(self._cache)
        else:
            self.metrics.inc_misses()
            return True

    def get_all_entities_changed(self, stream_pos):
        """Returns all entites that have had new things since the given
        position. If the position is too old it will return None.
        """
        assert type(stream_pos) is int

        if stream_pos >= self._earliest_known_stream_pos:
            return [self._cache[k] for k in self._cache.islice(
                start=self._cache.bisect_right(stream_pos))]
        else:
            return None

    def entity_has_changed(self, entity, stream_pos):
        """Informs the cache that the entity has been changed at the given
        position.
        """
        assert type(stream_pos) is int

        if stream_pos > self._earliest_known_stream_pos:
            old_pos = self._entity_to_key.get(entity, None)
            if old_pos is not None:
                stream_pos = max(stream_pos, old_pos)
                self._cache.pop(old_pos, None)
            self._cache[stream_pos] = entity
            self._entity_to_key[entity] = stream_pos

            while len(self._cache) > self._max_size:
                k, r = self._cache.popitem(0)
                self._earliest_known_stream_pos = max(
                    k, self._earliest_known_stream_pos,
                )
                self._entity_to_key.pop(r, None)

    def get_max_pos_of_last_change(self, entity):
        """Returns an upper bound of the stream id of the last change to an
        entity.
        """
        return self._entity_to_key.get(entity, self._earliest_known_stream_pos)