Skip to content

Instantly share code, notes, and snippets.

@ta1hia
Created March 10, 2016 19:58
Show Gist options
  • Save ta1hia/7fbf591132240bcdadff to your computer and use it in GitHub Desktop.
Save ta1hia/7fbf591132240bcdadff to your computer and use it in GitHub Desktop.
# 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