예제 #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))))
예제 #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))))
예제 #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")
    )
예제 #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')
예제 #5
0
def should_sort():
    assert [tabify('foo bar baz cux zip')] == list(format_connections(
        consolidate(tabify(['foo zip baz', 'foo bar cux']))))
예제 #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']) )
예제 #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)
예제 #8
0
def merge(onto, from_):
    return consolidate(
        sorted(
            extend_paths(
                sorted(combine(chain(onto, format_connections(from_)))))))
예제 #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']))
예제 #10
0
def should_sort():
    assert [tabify('foo bar baz cux zip')] == list(
        format_connections(consolidate(tabify(['foo zip baz',
                                               'foo bar cux']))))
예제 #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)
예제 #12
0
def merge(onto, from_):
    return consolidate(sorted(extend_paths(sorted(combine(
        chain(onto, format_connections(from_)))))))