I tried solving this problem on hackerrank where you are given a list a
and an integer d'
, you should rotate the list a
to left d
times and return the rotated list.
But, when I used this python code for a =[1,2,3,4,5]
and d = 4
I got the output as [1,1,1,1,1]
instead of [5,1,2,3,4]
.
temp = a
for j in range(len(a)):
a[j-d] = temp[j]
return a
But when I explicitly copied each element of list 'a' into the list 'temp' it worked fine and I passed all the test cases.
temp = []
for i in range(len(a)):
temp.append(a[i])
for j in range(len(a)):
a[j-d]=temp[j]
return a
Can somebody explain what was wrong with the earlier code?? Thank you!