Exemplo n.º 1
0
def orchestrate_logn(network, n):
    dp_table = {}
    for i in range(int(log2(n)) + 1):
        network = list(consolidate(sorted(path_double(network))))
        cur_pow2 = 2 ** i
        if n & cur_pow2 == cur_pow2: #only store if pow2 bit is set in n
            dp_table[cur_pow2] = network
    if len(dp_table) == 1:
        return format_connections(dp_table[n])
    return format_connections(reduce(merge, (dp_table[i] for i in sorted(dp_table.keys(), reverse = True))))
Exemplo n.º 2
0
def orchestrate_logn(network, n):
    dp_table = {}
    for i in range(int(log2(n)) + 1):
        network = list(consolidate(sorted(path_double(network))))
        cur_pow2 = 2**i
        if n & cur_pow2 == cur_pow2:  #only store if pow2 bit is set in n
            dp_table[cur_pow2] = network
    if len(dp_table) == 1:
        return format_connections(dp_table[n])
    return format_connections(
        reduce(merge,
               (dp_table[i] for i in sorted(dp_table.keys(), reverse=True))))
Exemplo n.º 3
0
def should_combine():
    n2 = tabify(
        """1 2 # 3 4
2 1 3 4 # 5 6
3 2 6 # 1 4
4 2 5 # 1 3
5 4 # 2
6 3 # 2
"""
    ).split("\n")
    n1 = tabify(
        """1 2 #
2 1 3 4 #
3 2 6 #
4 2 5 #
5 4 #
6 3 #
"""
    ).split("\n")
    n3 = list(format_connections(merge(n2, n1)))
    assert (
        n3
        == tabify(
            """1 2 3 4 5 6
2 1 3 4 5 6
3 1 2 4 5 6
4 1 2 3 5 6
5 1 2 3 4
6 1 2 3 4"""
        ).split("\n")
    )
Exemplo n.º 4
0
def should_combine():
    n2 = tabify('''1 2 # 3 4
2 1 3 4 # 5 6
3 2 6 # 1 4
4 2 5 # 1 3
5 4 # 2
6 3 # 2
''').split('\n')
    n1 = tabify('''1 2 #
2 1 3 4 #
3 2 6 #
4 2 5 #
5 4 #
6 3 #
''').split('\n')
    n3 = list(format_connections(merge(n2, n1)))
    assert n3 == tabify('''1 2 3 4 5 6
2 1 3 4 5 6
3 1 2 4 5 6
4 1 2 3 5 6
5 1 2 3 4
6 1 2 3 4''').split('\n')
Exemplo n.º 5
0
def should_sort():
    assert [tabify('foo bar baz cux zip')] == list(format_connections(
        consolidate(tabify(['foo zip baz', 'foo bar cux']))))
Exemplo n.º 6
0
def should_consolidate():
    assert (list(format_connections(consolidate(tabify(['1 2 3', '1 4 5',
                                                '2 3 1', '2 5 6', '3 1 2'])))) ==
            tabify(['1 2 3 4 5', '2 1 3 5 6', '3 1 2']) )
Exemplo n.º 7
0
def orchestrate_O_of_n(network, n):
    for i in range(n):
        network = list(consolidate(sorted(one_hop(network))))
    return format_connections(network)
Exemplo n.º 8
0
def merge(onto, from_):
    return consolidate(
        sorted(
            extend_paths(
                sorted(combine(chain(onto, format_connections(from_)))))))
Exemplo n.º 9
0
def should_consolidate():
    assert (list(
        format_connections(
            consolidate(tabify(['1 2 3', '1 4 5', '2 3 1', '2 5 6',
                                '3 1 2'])))) == tabify(
                                    ['1 2 3 4 5', '2 1 3 5 6', '3 1 2']))
Exemplo n.º 10
0
def should_sort():
    assert [tabify('foo bar baz cux zip')] == list(
        format_connections(consolidate(tabify(['foo zip baz',
                                               'foo bar cux']))))
Exemplo n.º 11
0
def orchestrate_O_of_n(network, n):
    for i in range(n):
        network = list(consolidate(sorted(one_hop(network))))
    return format_connections(network)
Exemplo n.º 12
0
def merge(onto, from_):
    return consolidate(sorted(extend_paths(sorted(combine(
        chain(onto, format_connections(from_)))))))