コード例 #1
0
def split(arr):
    evens = arrays.Array((len(arr) + 1) // 2)
    odds = arrays.Array(len(arr) // 2)
    isEven = True
    for i in range(len(arr)):
        if isEven:
            evens[i // 2] = arr[i]
        else:
            odds[i // 2] = arr[i]
        isEven = not isEven
    return evens, odds
コード例 #2
0
def list_to_array(my_list):
    """
    This function takes a list and returns an array of that list
    """
    my_array = arrays.Array(len(my_list))
    for index in range(len(my_list)):
        my_array[index] = my_list[index]
    return my_array
コード例 #3
0
def arrays_from_file(filename):
    length = get_file_length(filename)
    an_array = arrays.Array(length)
    with open(filename) as f:
        n = 0
        for l in f:
            stripped = l.strip()
            an_array[n] = stripped
            n += 1
    return an_array
コード例 #4
0
def arrays_from_file(filename):
    length = get_file_length(filename)
    file_array = arrays.Array(length)
    count = 0
    with open(filename) as a_file:
        for line in a_file:
            line = line.strip()
            file_array[count] = line
            count += 1
    return file_array
コード例 #5
0
def array(s):
    """Convert s as a hex string into an initialised array"""
    if len(s) % 2 != 0:
        s = '0' + s
    l = []
    for i in range(len(s) / 2):
        o = i * 2
        b = s[o:o + 2]
        n = fromstr(b)
        l.append(n)
    return arrays.Array(l)  # construct a new array
コード例 #6
0
def square_elements(an_array):
    array = arrays.Array(len(an_array) * 2)
    #counter = 0
    # for i in range(len(an_array)):
    #     array[i] = an_array[i] ** 2
    #     counter += 1
    # for i in range(len(an_array)):
    #     array[counter] = an_array[i]
    #     counter += 1
    # or
    counter = len(an_array)
    for i in range(len(an_array)):
        array[i] = an_array[i]**2
        array[i + counter] = an_array[i]
    return array
コード例 #7
0
def merge(half1, half2):
    result = arrays.Array(len(half1) + len(half2))
    i1 = 0
    i2 = 0
    while i1 < len(half1) and i2 < len(half2):
        if half1[i1] <= half2[i2]:
            result[i1 + i2] = half1[i1]
            i1 += 1
        else:
            result[i1 + i2] = half2[i2]
            i2 += 1
    if i1 < len(half1):
        for j in range(i1, len(half1)):
            result[i2 + j] = half1[j]
    else:
        for j in range(i2, len(half2)):
            result[i1 + j] = half2[j]

    return result
コード例 #8
0
def list_to_array(my_list):
    array_size = len(my_list)
    my_array = arrays.Array(array_size)
    for i in range(array_size):
        my_array[i] = my_list[i]
    return my_array