Question: https://codility.com/demo/take-sample-test/cyclic_rotation/

Question Name: Cyclic-Rotation or CyclicRotation

With Python’s syntactic sugar, the solution is pretty short.

1 2 3 4 5 6 7 | def solution(A, K): # write your code in Python 2.7 if len(A) == 0: return A K = K % len(A) return A[-K:] + A[:-K] |

Well, kind of too easy. ~~Let’s try a C++ solution to demo the details better.~~

Thanks to @micropentium6, the original C++ solution is ugly and bad. Please refer to the solution from @micropentium6. Also another Python solution with the same method as @micropentium6’s can be found in an older post.

With all due respect, your C++ solution is still like Python tricks. By pushing back, the vector is extended through newly allocated space. You can, actually, do this in place with or without vector.

I see~ I forgot this classic algorithm, which was mentioned in some old post. I was in the world of Python 🙂

hi sheng,

your code output is this:

INPUT: Array = [3, 8, 9, 7, 6]

k = 3

OUTPUT:[3, 9, 9, 7, 9, 3, 9]

I cannot understand your statement. The code should work.

The rotate function is missing~

It is a standard function:

http://en.cppreference.com/w/cpp/algorithm/rotate

Thanks for pointing it out 🙂

Javascript Solution

My Javascript solution:

Excelente!!, clean and straighfoward!

Here is what I came up with for a C# solution. It scored 100% on Codility:

https://codility.com/demo/results/training4T4DUN-KVN/

Hello, I am trying to understand your solution and I have to question that I do not find the logic. Please could illustrate me about it or where I can read more details about your code.

The questions are comment on the solution. Thank you in advances

def solution(A, K):

# write your code in Python 2.7

if len(A) == 0:# why do you equal to cero len(A)?

return A

K = K % len(A)# Why do you module K % len(A)

return A[-K:] + A[:-K]

Hello, they are mostly some mathematic issues.

>> why do you equal to cero len(A)?

A: handle the special case. Otherwise, when len(A) is 0, “K = K % len(A)” is illegal.

>> Why do you module K % len(A)

A: To make sure K is in the range of valid index (between 0 and len(A) – 1). The original question is rotation, so if we increase or decrease K by len(A), the result is totally the same.

My C solution :

https://codility.com/demo/results/training9N28CG-KWS/

My C solution:

My Java solution (100%)

Do we really need to shift array n times ? Instead we only need to figure out what is the index of the array would become the first element of new array, and then copy old array to new array. The following are my java solution.

No, n-times shift is totally a waste. FYI, your solution needs additional O(N) space, which is not necessary. Please refer to @micropentium6 solution.

this scored 100% too

Please read the “Guideline for Comments” in the right column first to post code. Thanks!

My Python solution

My soulition in php, got 100%:

Hi Ahmed,

Cannot we use array functions to get the solution?

My C++ solution

My Solution (Python)

My C# Code 100% works