Task:
An array
A
consisting ofN
integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of arrayA = [3, 8, 9, 7, 6]
is[6, 3, 8, 9, 7]
(elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate arrayA
K
times; that is, each element ofA
will be shifted to the rightK
times.
I was wondering why this solution is not working?
def solution(A , K):
old = A
new = [0]*len(A)
for i in range(K):
new[0]=old[-1]
new[1:] = old[:-1]
old = new
return new
Note: I have already solved the task, but I just don't understand why this is not working.