Codewar-010 Rotate Array
Codewar-010 Rotate Array

Codewar-010 Rotate Array
• 发表于 2年前
• 阅读 6
• 收藏 0
• 评论 0

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:

``````Object[] data = new Object[]{1, 2, 3, 4, 5};

rotate(data, 1)    =>    {5, 1, 2, 3, 4}
rotate(data, 2)    =>    {4, 5, 1, 2, 3}
rotate(data, 3)    =>    {3, 4, 5, 1, 2}
rotate(data, 4)    =>    {2, 3, 4, 5, 1}
rotate(data, 5)    =>    {1, 2, 3, 4, 5}

rotate(data, 0)    =>    {1, 2, 3, 4, 5}

rotate(data, -1)    =>    {2, 3, 4, 5, 1}
rotate(data, -2)    =>    {3, 4, 5, 1, 2}
rotate(data, -3)    =>    {4, 5, 1, 2, 3}
rotate(data, -4)    =>    {5, 1, 2, 3, 4}
rotate(data, -5)    =>    {1, 2, 3, 4, 5}``````

Furthermore the method should take ANY array of objects and perform this operation on them:

``````rotate(new Object[]{'a', 'b', 'c'}, 1)        =>    {'c', 'a', 'b'}
rotate(new Object[]{1.0, 2.0, 3.0}, 1)        =>    {3.0, 1.0, 2.0}
rotate(new Object[]{true, true, false}, 1)    =>    {false, true, true}``````

Finally 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:

``````Object[] data = new Object[]{1, 2, 3, 4, 5}

rotate(data, 7)        =>    {4, 5, 1, 2, 3}
rotate(data, 11)       =>    {5, 1, 2, 3, 4}
rotate(data, 12478)    =>    {3, 4, 5, 1, 2}``````
``````package codewars;

public class Rotator {

public Object[] rotate(Object[] data, int n) {

if (n == 0) {
return data;
} else {
Object[] result = new Object[data.length];
int steps = n % data.length;
for (int i = 0; i < data.length; i++) {

result[i] = data[(i - steps >= 0 ? (i - steps) % data.length
: i - steps + data.length)];
}

return result;
}
}
}``````
``````package codewars;
import static org.junit.Assert.*;
import org.junit.*;

public class RotatorTest {
private Rotator rotator;

@Before
public void setUp() {
this.rotator = new Rotator();
}

@After
public void tearDown() {
this.rotator = null;
}

@Test
public void testRotateOne_ArrayOfFive() {
assertArrayEquals(new Object[]{5, 1, 2, 3, 4},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 1));
}

@Test
public void testRotateTwo_ArrayOfFive() {
assertArrayEquals(new Object[]{4, 5, 1, 2, 3},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 2));
}

@Test
public void testRotateThree_ArrayOfFive() {
assertArrayEquals(new Object[]{3, 4, 5, 1, 2},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 3));
}

@Test
public void testRotateFour_ArrayOfFive() {
assertArrayEquals(new Object[]{2, 3, 4, 5, 1},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 4));
}

@Test
public void testRotateFive_ArrayOfFive() {
assertArrayEquals(new Object[]{1, 2, 3, 4, 5},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 5));
}

@Test
public void testRotateSix_ArrayOfFive() {
assertArrayEquals(new Object[]{5, 1, 2, 3, 4},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, 6));
}

@Test
public void testRotateNegOne_ArrayOfFive() {
assertArrayEquals(new Object[]{2, 3, 4, 5, 1},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -1));
}

@Test
public void testRotateNegTwo_ArrayOfFive() {
assertArrayEquals(new Object[]{3, 4, 5, 1, 2},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -2));
}

@Test
public void testRotateNegThree_ArrayOfFive() {
assertArrayEquals(new Object[]{4, 5, 1, 2, 3},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -3));
}

@Test
public void testRotateNegFour_ArrayOfFive() {
assertArrayEquals(new Object[]{5, 1, 2, 3, 4},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -4));
}

@Test
public void testRotateNegFive_ArrayOfFive() {
assertArrayEquals(new Object[]{1, 2, 3, 4, 5},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -5));
}

@Test
public void testRotateNegSix_ArrayOfFive() {
assertArrayEquals(new Object[]{2, 3, 4, 5, 1},
rotator.rotate(new Object[]{1, 2, 3, 4, 5}, -6));
}
}``````

https://www.codewars.com/kata/5469e0798a3502f4a90005c9/train/java

Jake from Codewars

Active in the last 15m

# 7 kyu - Rank earned!

A whole world awaits. You’re making coding moves – keep it up. With weekly practice, persistent focus and a kata a day, comes Mastery.

Why don't you share your awesomeness?

×