当前位置: 首页>>代码示例>>Python>>正文


Python Array.indexed方法代码示例

本文整理汇总了Python中datastructures.array.Array.indexed方法的典型用法代码示例。如果您正苦于以下问题:Python Array.indexed方法的具体用法?Python Array.indexed怎么用?Python Array.indexed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在datastructures.array.Array的用法示例。


在下文中一共展示了Array.indexed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: merge_

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def merge_(A, p, q, r):
    n1 = q - p + 1
    n2 = r - q
    L = Array.indexed(1, n1)
    R = Array.indexed(1, n2)
    for i in between(1, n1):
        L[i] = A[p + i - 1]
    for j in between(1, n2):
        R[j] = A[q + j]
    i = j = 1
    k = p
    while i <= n1 and j <= n2:
        if L[i] <= R[j]:
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1
        k = k + 1
    while i <= n1:
        A[k] = L[i]
        i = i + 1
        k = k + 1
    while j <= n2:
        A[k] = R[j]
        j = j + 1
        k = k + 1
开发者ID:wojtask,项目名称:CormenPy,代码行数:29,代码来源:exercise2_3_2.py

示例2: fastest_way_

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def fastest_way_(a, t, e, x, n):
    f = Array([Array.indexed(1, 2), Array.indexed(1, 2)])
    l = Array([Array.indexed(1, n), Array.indexed(1, n)])
    f[1, 2] = e[1] + a[1, 1]
    f[2, 2] = e[2] + a[2, 1]
    for j in between(2, n):
        f[1, 1] = f[1, 2]
        f[2, 1] = f[2, 2]
        if f[1, 1] + a[1, j] <= f[2, 1] + t[2, j - 1] + a[1, j]:
            f[1, 2] = f[1, 1] + a[1, j]
            l[1, j] = 1
        else:
            f[1, 2] = f[2, 1] + t[2, j - 1] + a[1, j]
            l[1, j] = 2
        if f[2, 1] + a[2, j] <= f[1, 1] + t[1, j - 1] + a[2, j]:
            f[2, 2] = f[2, 1] + a[2, j]
            l[2, j] = 2
        else:
            f[2, 2] = f[1, 1] + t[1, j - 1] + a[2, j]
            l[2, j] = 1
    if f[1, 2] + x[1] <= f[2, 2] + x[2]:
        f_star = f[1, 2] + x[1]
        l_star = 1
    else:
        f_star = f[2, 2] + x[2]
        l_star = 2
    return f_star, l, l_star
开发者ID:wojtask,项目名称:CormenPy,代码行数:29,代码来源:exercise15_1_4.py

示例3: get_random_multiple_array_list

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def get_random_multiple_array_list(min_size=1, max_size=10, max_value=999):
    list_size = random.randint(min_size, max_size)
    array_size = random.randint(list_size, max_size)
    key, next, prev = Array.indexed(1, array_size), Array.indexed(1, array_size), Array.indexed(1, array_size)
    list_indexes = random.sample(range(1, array_size + 1), list_size)

    head = None
    prev_index = None
    for index in list_indexes:
        key[index] = random.randint(0, max_value)
        if prev_index is None:
            head = index
        else:
            next[prev_index] = index
            prev[index] = prev_index
        prev_index = index

    free_indexes = [i for i in range(1, array_size + 1) if i not in list_indexes]
    random.shuffle(free_indexes)

    free = None
    prev_free_index = None
    for free_index in free_indexes:
        if prev_free_index is None:
            free = free_index
        else:
            next[prev_free_index] = free_index
        prev_free_index = free_index

    return MultipleArrayList(key, next, prev, head, free)
开发者ID:wojtask,项目名称:CormenPy,代码行数:32,代码来源:list_util.py

示例4: setup_activities

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def setup_activities(n):
    start_times = Array.indexed(0, n + 1)
    finish_times = Array.indexed(0, n + 1)
    for i in between(1, n):
        start_times[i] = random.randint(0, 49)
        finish_times[i] = start_times[i] + random.randint(1, 50)
    start_times[0], finish_times[0] = 0, 0
    start_times[n + 1], finish_times[n + 1] = math.inf, math.inf
    return start_times, finish_times
开发者ID:wojtask,项目名称:CormenPy,代码行数:11,代码来源:test_textbook16_1.py

示例5: make_change

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def make_change(n, d):
    c = Array.indexed(0, n)
    denom = Array.indexed(1, n)
    c[0] = 0
    for j in between(1, n):
        c[j] = math.inf
        for i in between(1, d.length):
            if j >= d[i] and 1 + c[j - d[i]] < c[j]:
                c[j] = 1 + c[j - d[i]]
                denom[j] = d[i]
    return c, denom
开发者ID:wojtask,项目名称:CormenPy,代码行数:13,代码来源:problem16_1.py

示例6: get_random_huge_array

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def get_random_huge_array(max_value=999):
    table_size = max_value
    table_capacity = random.randint(1, min(20, max_value))
    nelements = random.randint(0, table_capacity)
    table = Array.indexed(0, table_size - 1)
    stack = Array.indexed(1, table_capacity)
    keys = random.sample(range(max_value), nelements)

    for i, key in enumerate(keys):
        table[key] = i + 1
        stack[i + 1] = Element(key)
    stack.top = len(keys)

    return table, stack, keys
开发者ID:wojtask,项目名称:CormenPy,代码行数:16,代码来源:hash_table_util.py

示例7: dynamic_activity_selector

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def dynamic_activity_selector(s, f):
    n = s.length - 2
    c = Array([Array.indexed(0, n + 1) for _ in between(0, n + 1)])
    A = Array([Array.indexed(0, n + 1) for _ in between(0, n + 1)])
    for l in between(2, n + 2):
        for i in between(0, n - l + 2):
            j = i + l - 1
            c[i, j] = 0
            A[i, j] = set()
            for k in between(i + 1, j - 1):
                q = c[i, k] + c[k, j] + 1
                if f[i] <= s[k] < f[k] <= s[j] and q > c[i, j]:
                    c[i, j] = q
                    A[i, j] = A[i, k] | {'a' + str(k)} | A[k, j]
    return A[0, n + 1]
开发者ID:wojtask,项目名称:CormenPy,代码行数:17,代码来源:exercise16_1_1.py

示例8: greedy_make_change

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def greedy_make_change(n):
    C = Array.indexed(1, 6)
    d = Array([1, 2, 5, 10, 20, 50])
    for i in rbetween(d.length, 1):
        C[i] = math.floor(n / d[i])
        n %= d[i]
    return C
开发者ID:wojtask,项目名称:CormenPy,代码行数:9,代码来源:problem16_1.py

示例9: permute_by_sorting

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def permute_by_sorting(A):
    n = A.length
    P = Array.indexed(1, n)
    for i in between(1, n):
        P[i] = random(1, n ** 3)
    _sort_using_priorities(A, P)
    return A
开发者ID:wojtask,项目名称:CormenPy,代码行数:9,代码来源:textbook5_3.py

示例10: monge_minimums

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def monge_minimums(A):
    m = A.rows
    minimums_indices = _monge_minimums_indices(A)
    minimums = Array.indexed(1, m)
    for i in between(1, m):
        minimums[i] = A[i, minimums_indices[i]]
    return minimums
开发者ID:wojtask,项目名称:CormenPy,代码行数:9,代码来源:problem4_7.py

示例11: assert_valid_operations

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def assert_valid_operations(operations, word1, word2):
    i = 1
    j = 1
    m = word1.length
    n = word2.length
    result = Array.indexed(1, n)
    for op in operations:
        if op == 'copy':
            result[j] = word1[i]
            i += 1
            j += 1
        elif op[:11] == 'replace by ':
            ch = op[11:]
            result[j] = ch
            i += 1
            j += 1
        elif op == 'delete':
            i += 1
        elif op[:7] == 'insert ':
            ch = op[7:]
            result[j] = ch
            j += 1
        elif op == 'twiddle':
            result[j] = word1[i + 1]
            result[j + 1] = word1[i]
            i += 2
            j += 2
        else:
            assert_that(op, is_(equal_to('kill')))
            assert_that(op, is_(equal_to(operations[-1])))
            i = m + 1
    assert_that(i, is_(equal_to(m + 1)))
    assert_that(result, is_(equal_to(word2)))
开发者ID:wojtask,项目名称:CormenPy,代码行数:35,代码来源:test_problem15_3.py

示例12: matrix_chain_order

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def matrix_chain_order(p):
    n = p.length - 1
    m = Array([Array.indexed(1, n) for _ in between(1, n)])
    s = Array([Array.indexed(1, n) for _ in between(1, n)])
    for i in between(1, n):
        m[i, i] = 0
    for l in between(2, n):
        for i in between(1, n - l + 1):
            j = i + l - 1
            m[i, j] = math.inf
            for k in between(i, j - 1):
                q = m[i, k] + m[k + 1, j] + p[i - 1] * p[k] * p[j]
                if q < m[i, j]:
                    m[i, j] = q
                    s[i, j] = k
    return m, s
开发者ID:wojtask,项目名称:CormenPy,代码行数:18,代码来源:textbook15_2.py

示例13: get_random_single_array_list

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def get_random_single_array_list(min_size=1, max_size=10, max_value=999):
    list_size = random.randint(min_size, max_size)
    array_size = 3 * random.randint(list_size, max_size)
    A = Array.indexed(1, array_size)
    list_indexes = random.sample(range(1, array_size + 1, 3), list_size)

    head = None
    prev_index = None
    for index in list_indexes:
        A[index] = random.randint(0, max_value)
        if prev_index is None:
            head = index
        else:
            A[prev_index + 1] = index
            A[index + 2] = prev_index
        prev_index = index

    free_indexes = [i for i in range(1, array_size + 1, 3) if i not in list_indexes]
    random.shuffle(free_indexes)

    free = None
    prev_free_index = None
    for free_index in free_indexes:
        if prev_free_index is None:
            free = free_index
        else:
            A[prev_free_index + 1] = free_index
        prev_free_index = free_index

    return SingleArrayList(A, head, free)
开发者ID:wojtask,项目名称:CormenPy,代码行数:32,代码来源:list_util.py

示例14: memoized_matrix_chain

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def memoized_matrix_chain(p):
    n = p.length - 1
    m = Array([Array.indexed(1, n) for _ in between(1, n)])
    for i in between(1, n):
        for j in between(i, n):
            m[i, j] = math.inf
    return lookup_chain(p, m, 1, n)
开发者ID:wojtask,项目名称:CormenPy,代码行数:9,代码来源:textbook15_3.py

示例15: _construct_secondary_hash_table_no_collisions

# 需要导入模块: from datastructures.array import Array [as 别名]
# 或者: from datastructures.array.Array import indexed [as 别名]
def _construct_secondary_hash_table_no_collisions(keys, size, h_):
    S = Array.indexed(0, size - 1)
    for k in keys:
        if S[h_(k)] is not None:
            return None
        S[h_(k)] = k
    return S
开发者ID:wojtask,项目名称:CormenPy,代码行数:9,代码来源:textbook_problem11_5.py


注:本文中的datastructures.array.Array.indexed方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。