Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Last active January 27, 2017 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ru-rocker/723b630249441a03170b85ee87b2d45c to your computer and use it in GitHub Desktop.
Save ru-rocker/723b630249441a03170b85ee87b2d45c to your computer and use it in GitHub Desktop.
codility example
def solution(N):
if N <= 0:
return
elif N == 1:
return 0
val = N
idx = 1
max = 0
first = False
if N % 2 == 0:
first = True
while val > 0:
val = N >> idx
if val % 2 == 0:
idx += 1
elif max < idx:
if not first:
max = idx
idx = 1
N = val
else:
first = False
N = val
idx = 1
max = 1
else:
idx = 1
N = val
first = False
return max - 1
__author__ = 'ricky'
def solution(A, K):
l = len(A)
if l == 0:
return []
start = -1 * (K % l)
return A[start:] + A[0:start]
if __name__ == '__main__':
A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(solution(A, 3))
A = [3, 8, 9, 7, 6]
print(solution(A, 2))
print(solution(A, 7))
public class CyclicRotation {
public int[] solution(int[] A, int K) {
int [] result = new int[A.length];
int idx = K % A.length;
for(int i = 0 ; i < idx; i++){
result[i] = A[A.length - idx + i];
}
for(int j = idx; j < A.length; j++){
result[j] = A[j - idx];
}
for(int i = 0; i < result.length; i++){
System.out.print(result[i] + " ");
}
return result;
}
public static void main(String[] args) {
int [] a = new int[]{3, 8, 9, 7, 6};
CyclicRotation cr = new CyclicRotation();
cr.solution(a, 1);
System.out.println();
cr.solution(a, 3);
System.out.println();
cr.solution(a, 2);
System.out.println();
cr.solution(a, 7);
System.out.println();
cr.solution(a, 5);
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment