Skip to main content

Codility Lesson 2: Arrays CyclicRotation (Solution in Python)

Question: Given a list of length N, print the elements of the list A after K right cyclic rotations
Description of Input Data: 
1. N,K are integers in the range of [0,100]
2. Elements in A lie in the range of [-1000,1000]

Example: 
Input Data: A=[3,8,9,7,6], N=5, K=3
Expected output: [9,7,6,3,8]

Logic:
1. Elements in the list A need to be reordered according to the value of K and length of list A
2. K elements from the end of the list are the first to appear
3. The remaining elements in the same order are placed after the K elements from the end of the list

Psedo Code:
1. Check the length of the input list( length = 0 or length > K or length < 0)
2. If length = 0 output is []
3. If length > K: then the output is concatination of A[-K:],A[:-K]
4. If length < K: then the output is concatination of A[-(K%length) :], A[: -(K%length)]

https://codility.com/programmers/lessons/2-arrays/cyclic_rotation/
Let us use some test cases to understand the functioning of the code:
Test Case 1:
When length of the input list is less than K
Input List: A = [3,8,9,7,6], K=3
Step 1: Compare length of the list to value of K
Length of A = 5 < K=3
Step 2: Building the output which is concatenation of A[-K : ],A[ : -K]
Using this picture here, which represents how indices in python are for accessing elements are defined, the solution can be easily identified.
A[-K : ] = A[-3 : ] = [9,7,6] and A[ : -K] = A[ : -3] = [3,8]
Concatenation of A[-K : ] and A[ : -K] = Concatenation of [9,7,6] and [3,8] = [9,7,6,3,8]

Now since we know how to compute the solution, let us see how this is the solution to cyclic rotation of the list.
When K=1 the cyclic rotation of the list [3,8,9,7,6] = [6,3,8,9,7] - last element of the list is the first element after the rotation
When K = 2 the cyclic rotation of the list [3,8,9,7,6] = [7,6,3,8,9] - the last element of K=1 cyclic rotation output is the first element on this list
Similarly when K=3 the cyclic rotation of the list [3,8,9,7,6] = [9,7,6,3,8]

Test Case 2: When the length of the list is greater than value of K
From the understanding of cyclic rotation it is evident that once the value of K = length of the list the list will remain unchanged. We will use this fact to solve this test case.
Input List: A = [3,8,9,7,6] and K=18
Step 1: Compare length of list A to value of K
Length of A = 5 < K=18
Step 2: Compute the solution, which is concatenation of A[-(K%len(A)):] and A[:-(K%len(A))]
K%len(A) = 18%5 = 3
This means A[-(K%len(A)):] and A[:-(K%len(A))] = A[-(3):] and A[:-(3)]
So the solution as discussed above will be [9,7,6,3,8]
Logically speaking with K=length of the A or multiples of length of A the cyclic rotation output becomes the input list A itself hence for all values of K > length A finding the cyclic rotation output of K%len(A) is equvalent to the finding the cyclic rotation value of K > len(A).

PS: You can also access this code @
https://github.com/samhithachalla/codilitysolutions/blob/codility-python-solutions-with-%3C100-score/CyclicRotation.py

How did you like my solution? Do you have a better and elegant way of completing this codility test? Is there a way I can improve my articles? Comment and let me know. Hope I helped a bit in your preparation. Looking forward to your next visit. :) Like always..Dont forget to like, share and subscribe.

Comments

Popular posts from this blog

What is a codility test?..and why do I need to take it?

Codility is a website designed for screening programmers. It caters to employers and job seekers/coding enthusiasts. The employers can avail a free trial and register to use the website as a testing platform for screening developers/software engineers and job seekers can use the lessons and challenges to improve coding skills. Since its established that codility is a technical recruitment site, let us understand what kind of tests they deliver? and which group of employers are most likely to use this site?   What are codility tests?  Codility tests are timed competitive programming questions used for developer/software engineer recruitment. They have a different levels of questions from basic to expert level. A typical test has three questions which have to be completed in the time specified. A test generally goes like this: First the website shows you how to use its interface for the test. Then a question along with an example appears on the left hand side, the timer (right top co