Created
March 10, 2016 19:58
-
-
Save ta1hia/7fbf591132240bcdadff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you can write to stdout for debugging purposes, e.g. | |
# print "this is a debug message" | |
def solution(A, K): | |
# write your code in Python 2.7 | |
result = [] | |
L = len(A) | |
if L <= 1 or not K or L == K: | |
return A | |
if L < K: | |
K = K % L | |
A_ = reverse(A) | |
L = reverse(A_[:K]) | |
R = reverse(A_[K:]) | |
if L: | |
result = result + L | |
if R: | |
result = result + R | |
return result | |
def reverse(A): | |
L = len(A) - 1 | |
i = 0 | |
if L < 0: return | |
while i <= L/2: | |
tmp = A[i] | |
A[i] = A[L - i] | |
A[L - i] = tmp | |
i += 1 | |
return A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment