本文整理汇总了Python中datastructures.array.Array类的典型用法代码示例。如果您正苦于以下问题:Python Array类的具体用法?Python Array怎么用?Python Array使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Array类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_
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
示例2: fastest_way_
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
示例3: get_random_multiple_array_list
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)
示例4: setup_activities
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
示例5: get_random_min_heap
def get_random_min_heap(ary=2, min_size=1, max_size=20, max_value=999):
size = random.randint(min_size, max_size)
keys = [random.randint(0, max_value)]
for i in range(1, size):
bound = keys[(i - 1) // ary]
keys.append(random.randint(bound, max_value))
heap = Array(keys)
heap.heap_size = size
return heap, keys
示例6: make_change
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
示例7: get_random_huge_array
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
示例8: test_reset
def test_reset(self):
k = random.randint(1, 8)
highest = random.randint(-1, k - 1)
if highest == -1:
elements = [0] * k
else:
elements = [random.randint(0, 1) for _ in range(highest)] + [1] + [0] * (k - 1 - highest)
array = Array(elements, start=0)
array.highest = highest
reset(array)
actual_zero = bits_to_number(array.elements)
assert_that(actual_zero, is_(equal_to(0)))
assert_that(array.highest, is_(equal_to(-1)))
示例9: dynamic_activity_selector
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]
示例10: assert_valid_operations
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)))
示例11: permute_by_sorting
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
示例12: greedy_make_change
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
示例13: memoized_matrix_chain
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)
示例14: _construct_secondary_hash_table_no_collisions
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
示例15: monge_minimums
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