Esempio n. 1
0
def _guess_k_smallest(tt: TourneyTree, k: int) -> [int]:
    ksmallest_guess = list()
    for row in range(tt.height - 1, -1, -1):
        for pos in tt.iter_row(row):
            if tt[pos] not in ksmallest_guess:
                ksmallest_guess.append(tt[pos])

            if len(ksmallest_guess) == k:
                return ksmallest_guess

    return ksmallest_guess
Esempio n. 2
0
def _guess_k_smallest_2(tt: TourneyTree, k: int) -> [int]:
    # item: (row on, row of partner)
    row_scores = dict()
    row_scores[tt[tt.top()]] = (tt.height - 1, tt.height - 1)

    for row in range(tt.height - 2, -1, -1):
        for pos in tt.iter_row(row):
            if tt[pos] not in row_scores:
                row_scores[tt[pos]] = (row,
                                       row_scores[tt[tt.get_sibling(pos)]][0])

        if len(row_scores) >= k:
            return sorted(row_scores, key=row_scores.__getitem__,
                          reverse=True)[:k]

    return sorted(row_scores, key=row_scores.__getitem__, reverse=True)[:k]
Esempio n. 3
0
def _populate_row(tt: TourneyTree, row: int):
    for pos in tt.iter_row(row):
        children = tt.get_children(pos)

        if len(children) == 1:
            tt[pos] = tt[children[0]]
            continue

        if len(children) == 0:
            tt[pos] = -1
            continue

        if len(children) != 2:
            raise Exception("should only be one or two children")

        tt[pos] = tt[children[0]] if COMPARE(
            tt[children[0]], tt[children[1]]) else tt[children[1]]
Esempio n. 4
0
def _guess_k_smallest_3(tt: TourneyTree, k: int) -> [int]:
    # item: (row on, row of partner)
    row_scores = dict()
    row_scores[tt[tt.top()]] = (tt.height - 1, tt.height - 1)

    sort_by = dict()
    sort_by[tt[tt.top()]] = 2 * (tt.height - 1)

    for row in range(tt.height - 2, -1, -1):
        for pos in tt.iter_row(row):
            if tt[pos] not in row_scores:
                row_scores[tt[pos]] = (row,
                                       row_scores[tt[tt.get_sibling(pos)]][0])
                # you can weight this 0.001 constant differently
                # doesn't seem fruitful though
                sort_by[tt[pos]] = row + 0.001 * row_scores[tt[tt.get_sibling(
                    pos)]][0]

    return sorted(row_scores, key=sort_by.__getitem__, reverse=True)[:k]
Esempio n. 5
0
def _get_element_positions(tt: TourneyTree, elements: [int]) -> {
        int: Position
}:
    elements_left = set(elements)
    element_positions = dict()

    for row in range(tt.height - 1, -1, -1):
        for pos in tt.iter_row(row):
            parent = tt.get_parent(pos)

            if tt[pos] not in elements_left:
                continue

            element_positions[tt[pos]] = pos
            elements_left.remove(tt[pos])

            if len(elements_left) == 0:
                return element_positions

    return element_positions