Skip to content

Instantly share code, notes, and snippets.

@Plotist
Last active December 26, 2020 21:21
Show Gist options
  • Save Plotist/1132ad5ee60685e9f042dbbdc7f3f121 to your computer and use it in GitHub Desktop.
Save Plotist/1132ad5ee60685e9f042dbbdc7f3f121 to your computer and use it in GitHub Desktop.
<codility> CyclicRotation
# 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