Dynamic Array

  • + 0 comments

    def ndimensional_list(dimensions):

    return [] if not dimensions else [ndimensional_list(dimensions[1:]) for _ in range(dimensions[0])]
    

    def dynamicArray(n, queries):

    arr = ndimensional_list([n])
    lastAnswer = 0
    n_arr = []
    for i in queries:
        q = i[0]
        x = i[1]
        y = i[2]
        if q == 1:
            idx = (x ^ lastAnswer) % n
            arr[idx].append(y)
        if q == 2:
            idx = (x ^ lastAnswer) % n
            idy = y % len(arr[idx])
            lastAnswer = arr[idx][idy]
            n_arr.append(lastAnswer)
    return n_arr