def __init__(self, chan, subject): self.posts = sortedlist(key=lambda post: post.timestamp) # postid -> set(replies) self.repliesByPostId = {} self.subject = subject self.timestamp = 0 self.threadid = getThreadId(subject) self.chan = chan
def prevPermOpt1(self, arr): n = len(arr) res = [arr[-1]] * n from sortedcontainers import sortedlist a = sortedlist() records = {} for i in range(n - 1, -1, -1): idx = a.bisect_left(arr[i]) # 找到 小于等于 i 的最大元素 a.append(arr[i]) records[arr[i]] = i if idx == 0: continue else: arr[i], arr[records[arr[idx - 1]]] = arr[records[arr[idx - 1]]], arr[i] return arr
def __init__(self, chan): self._threads = sortedlist(key=lambda thread: -thread.timestamp) self._threadsById = {} self.chan = chan
def split_equation_set(eqn_set): """Split an equation set up into smaller solvable equation sets""" # used for tiebreaker of priority key n_eq = len(eqn_set.eqns) + 1 solve_sets = set() # keep track of what has been visited unique_eqn_combos = set() unsolved_eqns = set(eqn_set.eqns) # Initialize priority queue with the equations in the input set pq = sortedlist([EqnSet().add(eqn) for eqn in eqn_set.eqns], key=methodcaller("key", n_eq)) while pq: eqn_set = pq.pop() if eqn_set.is_constrained(): # set this equation set as solved solve_sets.add(eqn_set) eqn_set.set_solved() unsolved_eqns.difference_update(eqn_set.eqns) # discard this equation set from all sets in the pq for p in pq: p.discard(eqn_set) # delete any empty eqn sets and re-sort the pq pq = sortedlist(filter(None, pq), key=methodcaller("key", n_eq)) unique_eqn_combos = set( frozenset(eqs.eqns | eqs.vars) for eqs in pq) else: # add the frontier to the pq for eqs in eqn_set.frontier(): eqn_combo = frozenset(eqs.eqns | eqs.vars) if eqn_combo not in unique_eqn_combos: unique_eqn_combos.add(eqn_combo) pq.add(eqs) # create eqn set(s) of underconstrained systems # TODO: can create multiple unconstrained sets if unsolved_eqns: underconstrained_set = EqnSet() for eqn in unsolved_eqns: underconstrained_set.add(eqn) underconstrained_set.set_solved() solve_sets.add(underconstrained_set) # while unsolved_eqns: # eqn = unsolved_eqns.pop() # eqn_set = EqnSet() # # connected_eqns = {eqn} # # while connected_eqns: # eqn = connected_eqns.pop() # unsolved_eqns.discard(eqn) # eqn_set.add(eqn) # # for var in eqn.vars: # connected_eqns.update(eqn for eqn in var.eqns # if eqn not in eqn_set.eqns) # # eqn_set.set_solved() # solve_sets.add(eqn_set) return solve_sets