-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicArray.py
52 lines (41 loc) · 1.24 KB
/
dynamicArray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the dynamicArray function below.
def dynamicArray(n, queries):
lastAns = 0
arr_of_lastAns = []
list_of_sequence = []
for i in range(n):
list_of_sequence.append([])
# print(list_of_sequence)
for arr in queries:
# print(arr[0])
# print(type(arr[0]))
if arr[0] == 1:
index = (arr[1] ^ lastAns) % n
list_of_sequence[index].append(arr[2])
elif arr[0] == 2:
sequence_no = (arr[1] ^ lastAns) % n
# print(len(list_of_sequence[sequence_no]))
element_index = arr[2] % len(list_of_sequence[sequence_no])
# print(element_index)
lastAns = list_of_sequence[sequence_no][element_index]
arr_of_lastAns.append(lastAns)
return arr_of_lastAns
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nq = input().rstrip().split()
n = int(nq[0])
q = int(nq[1])
queries = []
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
result = dynamicArray(n, queries)
# print(result)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()