Skip to content

Instantly share code, notes, and snippets.

@thdaraujo
Created April 7, 2018 18:17
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thdaraujo/6cc809b47a61403b4d9319ac768a2ad0 to your computer and use it in GitHub Desktop.
Cyclic rotation of array k times
def cyclic_rotation(a, k)
size = a.size
return a if k == size || k == 0
result = a.clone
a.each_with_index{|e, i|
result[(i + k) % size] = e
}
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment