Exemple #1
0
 def find_unbalanced(self):
     node = self
     while True:
         grouped_children = node.grouped('children')
         unbalanced = cc.pipe(grouped_children,
                              cc.valfilter(lambda x: len(x) == 1),
                              lambda x: cc.first(x.values())[0])
         if unbalanced.children_are_balanced:
             return unbalanced
         node = unbalanced
Exemple #2
0
 def indice2Arr(indice: Indice) -> Union[np.ndarray, float]:
     whichIn = insDispatcher(indice)[0]
     doFuncs: Dict[str, bool] = valmap(lambda v: bool(v(indice)[0]), funcsIndex)
     whichFunc = list(valfilter(identity, doFuncs).keys())
     if not whichFunc:
         func = lambda x: x
     else:
         if whichFunc[0] not in f2:
             raise ValueError("No function called {}.".format(whichFunc[0]))
         func = f2[whichFunc[0]]
     return func(
         self.getInputElement(inArrs[whichIn], tuple(indexConverters2[whichIn](indice)))
     )
Exemple #3
0
 def getIndexConverters(
         self, indexMap: Dict[Indice, LabelInIndice]
 ) -> List[IndexConverter]:
     return [
         self.getIndexConverter(
             i,
             compose(
                 valmap(lambda v: v[1]),
                 valfilter(lambda v, i=i: v[0] == i),
             )(indexMap),
         )
         for i in range(len(self.inAsts))
     ]
Exemple #4
0
        x[1], lambda x: x.split(','), cc.map(str.strip), list))), list)

tree_val_dict = cc.pipe(
    data_input, cc.map(cc.first),
    cc.map(lambda x: [tree_val_re.match(x).group(y) for y in (1, 2)]), dict,
    cc.valmap(int))

tree_mapping_dict = cc.pipe(
    data_input, cc.map(lambda x: (tree_val_re.match(x[0]).group(1), x[1])),
    dict)

root = cc.pipe(
    tree_mapping_dict.keys(),
    cc.filter(lambda x: x not in cc.concat(tree_mapping_dict.values())),
    cc.first)

tree = Tree(root, tree_mapping_dict, tree_val_dict)

unbalanced = tree.find_unbalanced()
unbalanced_self_weight = unbalanced.weight - sum(x.weight
                                                 for x in unbalanced.children)
unbalanced_grouped_siblings = unbalanced.grouped('siblings')

balanced_weight = cc.first(
    cc.valfilter(lambda x: len(x) > 1, unbalanced_grouped_siblings).keys())
unbalanced_weight = cc.first(
    cc.valfilter(lambda x: len(x) == 1, unbalanced_grouped_siblings).keys())
weight_offset = balanced_weight - unbalanced_weight

pp(unbalanced_self_weight + weight_offset)
    max_seen_index = None
    for i, value in enumerate(seq):
        if max_seen is None or value > max_seen:
            max_seen = value
            max_seen_index = i
    return max_seen_index


data_input = cc.pipe(sys.stdin.readline(), str.split, cc.map(int), list)

selected_bin = 0
dist = data_input
seen_dists_dict = {}
cycle_len_dict = {}

while not cc.valfilter(lambda x: x >= 2, seen_dists_dict):
    dist_tuple = tuple(dist)
    if dist_tuple not in seen_dists_dict:
        seen_dists_dict[dist_tuple] = 1
    else:
        seen_dists_dict[dist_tuple] += 1

    if dist_tuple not in cycle_len_dict:
        cycle_len_dict[dist_tuple] = 0
    cycle_len_dict = cc.valmap(lambda x: x + 1, cycle_len_dict)

    selected_bin = argmax(dist)
    selected_bin_val = dist[selected_bin]
    dist[selected_bin] = 0

    while selected_bin_val > 0: