Last active
July 5, 2016 09:27
-
-
Save jooeycheng/44b514249ddabb55d75a1943d238e1af to your computer and use it in GitHub Desktop.
Solutions to some of Codility Lessons
This file contains 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
# === [Lesson 2: Arrays] - CyclicRotation | |
# === Program v1 (recursive) | |
def solution(a, k) | |
return a if k == 0 || a.length == 0 | |
a = a.unshift(a.delete_at(a.length - 1)) | |
k -= 1 | |
return solution(a, k) | |
end | |
# === Program v2 (iterative) | |
def solution(a, k) | |
while k > 0 && a.length > 0 | |
a = a.unshift(a.delete_at(a.length - 1)) | |
k -= 1 | |
end | |
return a | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment