Skip to content

Instantly share code, notes, and snippets.

@kirylrb
Created April 19, 2020 12:43
Show Gist options
  • Save kirylrb/b55073cdc07639b065f58157b5448344 to your computer and use it in GitHub Desktop.
Save kirylrb/b55073cdc07639b065f58157b5448344 to your computer and use it in GitHub Desktop.
Rotation of the array in Ruby
# An array A consisting of N integers is given.
# Rotation of the array means that each element is shifted right by one index,
# and the last element of the array is moved to the first place.
# For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]
# (elements are shifted right by one index and 6 is moved to the first place).
def solution(a, k)
# write your code in Ruby 2.2
k.times do
a = [a.last] + a.shift(a.size-1)
end
return a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment