6 kyu
Rotate Array
1,643 of 2,210cjmcgraw
Description:
Create a method named "rotate" that returns a given array with the elements inside the array rotated n
spaces.
If n is greater than 0 it should rotate the array to the right. If n is less than 0 it should rotate the array to the left. If n is 0, then it should return the array unchanged.
Example:
with array [1, 2, 3, 4, 5]
n = 1 => [5, 1, 2, 3, 4]
n = 2 => [4, 5, 1, 2, 3]
n = 3 => [3, 4, 5, 1, 2]
n = 4 => [2, 3, 4, 5, 1]
n = 5 => [1, 2, 3, 4, 5]
n = 0 => [1, 2, 3, 4, 5]
n = -1 => [2, 3, 4, 5, 1]
n = -2 => [3, 4, 5, 1, 2]
n = -3 => [4, 5, 1, 2, 3]
n = -4 => [5, 1, 2, 3, 4]
n = -5 => [1, 2, 3, 4, 5]
The rotation shouldn't be limited by the indices available in the array. Meaning that if we exceed the indices of the array it keeps rotating.
Example:
with array [1, 2, 3, 4, 5]
n = 7 => [4, 5, 1, 2, 3]
n = 11 => [5, 1, 2, 3, 4]
n = 12478 => [3, 4, 5, 1, 2]
Arrays
Algorithms
Similar Kata:
Stats:
Created | Nov 17, 2014 |
Published | Nov 17, 2014 |
Warriors Trained | 4967 |
Total Skips | 709 |
Total Code Submissions | 7250 |
Total Times Completed | 2210 |
Java Completions | 1643 |
Python Completions | 499 |
C Completions | 52 |
Total Stars | 115 |
% of votes with a positive feedback rating | 95% of 315 |
Total "Very Satisfied" Votes | 285 |
Total "Somewhat Satisfied" Votes | 26 |
Total "Not Satisfied" Votes | 4 |