Last active
December 26, 2020 21:21
-
-
Save Plotist/1132ad5ee60685e9f042dbbdc7f3f121 to your computer and use it in GitHub Desktop.
<codility> CyclicRotation
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. | |
# puts "this is a debug message" | |
def solution(a, k) | |
# write your code in Ruby 2.2 | |
result = [] | |
n = a.length | |
raise ArgumentError.new( | |
"Invalid argument type" | |
) if !a.is_a?(Array) || !k.is_a?(Integer) | |
raise ArgumentError.new( | |
"n should be within [0..100]" | |
) if n > 100 | |
raise ArgumentError.new( | |
"k should be within [0..100]" | |
) if k > 100 || k < 0 | |
return [] if n == 0 | |
return a if k == 0 | |
shift = k%n | |
i = 0 | |
while i < n | |
raise ArgumentError.new( | |
"Invalid value in A" | |
) if a[i] < -1000 || a[i] > 1000 | |
index = i+shift | |
index = index-n if index > n-1 | |
result[index] = a[i] | |
i+=1 | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment