[Codility Series — Python solution] Lesson 2.1 CyclicRotation

Manish Joshi
DevOpsPerspective
Published in
3 min readDec 25, 2020

--

This post is to provide a solution for the Codility problem — Cyclic Rotation.

I would encourage you to read the problem statement carefully on the Codility website, try out your own solution first, and if you are really stuck even after spending considerable time on the problem, look for the solution provided below.

I will try to provide a detailed description of why I choose to write the solution a certain way but please note that I do not consider myself Python ‘Guru’ and hence would encourage you to try and make the solution even better and let me know your thoughts in the comments section as well :)

def solution(A, K):
if K == len(A) or len(A) == 0 or len(A) == 1:
return A
elif K > len(A):
K = K % len(A)
else:
pass
B = A[len(A)-K:]
C = A[:len(A)-K]
return B + C

Details on the Solution:

if K == len(A) or len(A) == 0 or len(A) == 1

The above “if statement” tries to catch the following straightforward cases early in the solution so that we do not have to waste the time and memory in processing these cases:

  1. If we rotate an array the same number of times as its length, we will always get the same array back. Example — [1, 2, 3] rotated 3 times will give us [3, 1, 2] → [2, 3, 1] → [1, 2, 3].
  2. If the array is empty, we will always get an empty array in the result.
  3. If the array has only 1 element, we will always get the same array back, no matter if we rotate it 10 times or 100, 000 times.
elif K > len(A):
K = K % len(A)
else:
pass

Since we know that if we rotate an array the same number of times as the length of the array we will always get the same array, so in case of a very high value for ‘K’ we could safely ignore the value of K that is in multiples of the length of the array. For example, if the length of an array is 80 and the value of ‘K’ is 82, instead of trying to rotate the array 82 times, we can rotate it just 82 % 80 = 2 times and get the same result. The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left-hand operand by the right-hand operand.

The program will enter the else state when K is already less than len(A) and in that case, we don’t have to do anything and hence we would just use pass.

B = A[len(A)-K:]
C = A[:len(A)-K]
return B + C

The final piece is just slicing coming to the rescue where we can create 2 arrays, the first one “B” which gives us array A that starts from len(A)-K index so for an array A = [1,2,3,4,5] and K =2, array B would start from index 5 -2 = 3, i.e. [4, 5]. Similarly, for the same example array, C with slicing will give us the elements of the array till index 3, i.e. C will be [1, 2, 3]. With the above 2 arrays set we can happily return array B + array C as the result, which will give us [4, 5, 1, 2, 3] which is the array A rotated 2 times.

Please note that we could have avoided assigning array B and array C altogether and wrote the slicing logic directly in the return statement like below.

return A[len(A)-K::]+ A[:len(A)-K]

--

--

Manish Joshi
DevOpsPerspective

Software engineer based out of London with 10+ years of DevOps experience in designing, building, and maintaining core infrastructure and platforms in the cloud