Jan-07-2021, 05:01 PM
I'm trying to write a code to rotate an array by a given value. Here's my code:
A = [4,6,8,9,1] K = 2 def rotate_array(A,K): for x in A: y = x + K if y >= len(A): y = y - len(A) A[x] = A[y] print(A) rotate_array(A,K)I'm getting this error:
Error: File "c:\Users\djwil\Documents\python\codility\cyclic_array.py", line 14, in <module>
rotate_array(A,K)
File "c:\Users\djwil\Documents\python\codility\cyclic_array.py", line 10, in rotate_array
A[y] = A[x]
IndexError: list index out of range
However I'm not sure why I'm getting this error as I wrote the lines below to stop this error from occuring:if y >= len(A): y = y - len(A)Can someone help me with this please?