How to Rotate Elements in an Array in Ruby

0
2486

In this coding exercise we’ll walk through how to easily rotate the items inside a Ruby array and change the index values for each element.

Summary

Build a method that dynamically rotates the indices for each of the values in a Ruby array.

Exercise File

Code File

Exercise Description

Build a method that takes two arguments: an array, and the number of indices that the array elements should be rotated.

Sample Use

array_rotation([1, 2, 3], 2) # [3, 1, 2]

Real World Usage

There are many times when you want to quickly rotate the elements in an array. This typically comes up when implementing algorithms that need to have flexible data structures.

Solution

Can be found on the solutions branch on github.

LEAVE A REPLY

Please enter your comment!
Please enter your name here