-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicarray.py
More file actions
90 lines (73 loc) · 2.19 KB
/
dynamicarray.py
File metadata and controls
90 lines (73 loc) · 2.19 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#https://www.hackerrank.com/challenges/dynamic-array/problem
# def dynamicArray(n, queries):
# # initialize a variable no of arrays..done
# rows = n
# arr = []
# ans=[]
# for i in range(0,n):
# arr.append([])
# lastAnswer=0
# for i in range(0,len(queries)):
# if queries[i][0]==1:
# idx=((queries[i][1]^lastAnswer)%n)
# #how to append an element to a nested 2d list..done
# arr[idx].append(queries[i][2])
# # print("arr is"+str(arr))
# if queries[i][0]==2:
# idx=((queries[i][1]^lastAnswer)%n)
# # print("idx is"+str(idx))
# lastAnswer=arr[idx][queries[i][2]%len(arr[idx])]
# # print(lastAnswer)
# ans.append(lastAnswer)
# # print(lastAnswer)
# queries=[[1, 0, 5],[1, 1, 7],[1, 0, 3],[2, 1, 0],[2, 1, 1]]
# n=2
# result = dynamicArray(n, queries)
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'dynamicArray' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER n
# 2. 2D_INTEGER_ARRAY queries
#
def dynamicArray(n, queries):
# Write your code here
ans=[]
# initialize a variable no of arrays..done
rows = n
arr = []
for i in range(0,n):
arr.append([])
lastAnswer=0
for i in range(0,len(queries)):
if queries[i][0]==1:
idx=((queries[i][1]^lastAnswer)%n)
#how to append an element to a nested 2d list..done
arr[idx].append(queries[i][2])
# print("arr is"+str(arr))
if queries[i][0]==2:
idx=((queries[i][1]^lastAnswer)%n)
# print("idx is"+str(idx))
lastAnswer=arr[idx][queries[i][2]%len(arr[idx])]
ans.append(lastAnswer)
# print(lastAnswer)
return ans
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
q = int(first_multiple_input[1])
queries = []
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
result = dynamicArray(n, queries)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()