We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
i had a problem with the variable idx, the problem says about calculate two idx, the first in the query one (idx = (x ^ last_answer)) and the second in query two (idx = (x ^ last_answer) % n) i solve the problem with variable idx = (x ^ last_answer) % n in the up of my for cycle and delete other idx
code:
def dynamicArray(n, queries):
array_2D = []
last_answer = 0
results = []
for i in range(len(queries)):
array_2D.append([])
for types,x,y in queries:
idx = (x ^ last_answer) % n
if types == 1:
#idx = (x ^ last_answer)
array_2D[idx].append(y)
else:
#idx = (x ^ last_answer) % n
last_answer = array_2D[idx][y % len(array_2D[idx])]
results.append(last_answer)
return results
result = dynamicArray(2, [[1,1,1],[1,1,7],[1,0,3],[2,1,0],[2,1,1]])
for i in result:
print(i)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Left Rotation
You are viewing a single comment's thread. Return to all comments →
i had a problem with the variable idx, the problem says about calculate two idx, the first in the query one (idx = (x ^ last_answer)) and the second in query two (idx = (x ^ last_answer) % n) i solve the problem with variable idx = (x ^ last_answer) % n in the up of my for cycle and delete other idx code:
def dynamicArray(n, queries): array_2D = [] last_answer = 0 results = [] for i in range(len(queries)): array_2D.append([]) for types,x,y in queries: idx = (x ^ last_answer) % n if types == 1: #idx = (x ^ last_answer) array_2D[idx].append(y) else: #idx = (x ^ last_answer) % n last_answer = array_2D[idx][y % len(array_2D[idx])] results.append(last_answer) return results