Skip to content

Commit f975986

Browse files
Added rotate array function
1 parent bf906dd commit f975986

File tree

6 files changed

+79
-28
lines changed

6 files changed

+79
-28
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ sbt test
1414
### Overview
1515

1616
* Arrays
17-
* [ReverseArray](src/main/java/algorithms/array/ReverseArray.java) => [test](src/test/scala/algorithms/array/ReverseArraySpec.scala)
17+
* [reverse](src/main/java/algorithms/array/ReverseArray.java) => [test](src/test/scala/algorithms/array/ReverseArraySpec.scala)
18+
* [rotate](src/main/java/algorithms/array/RotateArray.java) => [test](src/test/scala/algorithms/array/RotateArraySpec.scala)

Diff for: src/main/java/algorithms/array/ArrayUtil.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package algorithms.array;
2+
3+
public class ArrayUtil {
4+
5+
public static void print(int[] arr) {
6+
StringBuilder sb = new StringBuilder("[");
7+
for (int i = 0; i < arr.length; i++) {
8+
if (i > 0) {
9+
sb.append(", ");
10+
}
11+
12+
sb.append(arr[i]);
13+
}
14+
15+
sb.append("]");
16+
System.out.println(sb.toString());
17+
}
18+
}

Diff for: src/main/java/algorithms/array/ReverseArray.java

+1-15
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,9 @@ public static int[] reverse(int[] arr, int offset, int length) {
1212
arr[i] = arr[j];
1313
arr[j] = tmp;
1414

15-
//printArray(arr);
15+
//ArrayUtil.print(arr);
1616
}
1717

1818
return arr;
1919
}
20-
21-
static void printArray(int[] arr) {
22-
StringBuilder sb = new StringBuilder("[");
23-
for (int i = 0; i < arr.length; i++) {
24-
if (i > 0) {
25-
sb.append(", ");
26-
}
27-
28-
sb.append(arr[i]);
29-
}
30-
31-
sb.append("]");
32-
System.out.println(sb.toString());
33-
}
3420
}

Diff for: src/main/java/algorithms/array/RotateArray.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package algorithms.array;
2+
3+
public class RotateArray {
4+
5+
public static int[] rotate(int[] arr, int n) {
6+
if (arr.length > 0) {
7+
int d = n % arr.length;
8+
9+
ReverseArray.reverse(arr, 0, d);
10+
//ArrayUtil.print(arr);
11+
12+
ReverseArray.reverse(arr, d, arr.length - d);
13+
//ArrayUtil.print(arr);
14+
15+
ReverseArray.reverse(arr);
16+
//ArrayUtil.print(arr);
17+
}
18+
19+
return arr;
20+
}
21+
}

Diff for: src/test/scala/algorithms/array/ReverseArraySpec.scala

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ class ReverseArraySpec extends FlatSpec with Matchers {
77

88
it should "reverse the whole array" in {
99
//when & then
10-
reverse(Array.empty[Int]).toList shouldBe Nil
11-
reverse(Array(1)).toList shouldBe List(1)
12-
reverse(Array(1, 2)).toList shouldBe List(2, 1)
13-
reverse(Array(1, 2, 3, 4, 5)).toList shouldBe List(5, 4, 3, 2, 1)
10+
reverse(Array[Int]()) shouldBe Nil
11+
reverse(Array(1)) shouldBe List(1)
12+
reverse(Array(1, 2)) shouldBe List(2, 1)
13+
reverse(Array(1, 2, 3, 4, 5)) shouldBe List(5, 4, 3, 2, 1)
1414
}
1515

1616
it should "reverse part of the array" in {
1717
//when & then
18-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 0).toList shouldBe List(1, 2, 3, 4, 5, 6, 7)
19-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 1).toList shouldBe List(1, 2, 3, 4, 5, 6, 7)
20-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 2).toList shouldBe List(2, 1, 3, 4, 5, 6, 7)
21-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 3).toList shouldBe List(3, 2, 1, 4, 5, 6, 7)
22-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 1, 2).toList shouldBe List(1, 3, 2, 4, 5, 6, 7)
23-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 1, 4).toList shouldBe List(1, 5, 4, 3, 2, 6, 7)
24-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 5, 2).toList shouldBe List(1, 2, 3, 4, 5, 7, 6)
25-
reverse(Array(1, 2, 3, 4, 5, 6, 7), 6, 0).toList shouldBe List(1, 2, 3, 4, 5, 6, 7)
18+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 0) shouldBe List(1, 2, 3, 4, 5, 6, 7)
19+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 1) shouldBe List(1, 2, 3, 4, 5, 6, 7)
20+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 2) shouldBe List(2, 1, 3, 4, 5, 6, 7)
21+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 3) shouldBe List(3, 2, 1, 4, 5, 6, 7)
22+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 1, 2) shouldBe List(1, 3, 2, 4, 5, 6, 7)
23+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 1, 4) shouldBe List(1, 5, 4, 3, 2, 6, 7)
24+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 5, 2) shouldBe List(1, 2, 3, 4, 5, 7, 6)
25+
reverse(Array(1, 2, 3, 4, 5, 6, 7), 6, 0) shouldBe List(1, 2, 3, 4, 5, 6, 7)
2626
}
2727
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package algorithms.array
2+
3+
import RotateArray._
4+
import org.scalatest.{FlatSpec, Matchers}
5+
6+
class RotateArraySpec extends FlatSpec with Matchers {
7+
8+
it should "rotate array elements" in {
9+
//when & then
10+
rotate(Array[Int](), 1) shouldBe Nil
11+
rotate(Array(1), 1) shouldBe List(1)
12+
rotate(Array(1, 2), 0) shouldBe List(1, 2)
13+
rotate(Array(1, 2), 1) shouldBe List(2, 1)
14+
rotate(Array(1, 2), 2) shouldBe List(1, 2)
15+
rotate(Array(1, 2), 3) shouldBe List(2, 1)
16+
rotate(Array(1, 2), 4) shouldBe List(1, 2)
17+
rotate(Array(1, 2, 3, 4, 5), 0) shouldBe List(1, 2, 3, 4, 5)
18+
rotate(Array(1, 2, 3, 4, 5), 1) shouldBe List(2, 3, 4, 5, 1)
19+
rotate(Array(1, 2, 3, 4, 5), 2) shouldBe List(3, 4, 5, 1, 2)
20+
rotate(Array(1, 2, 3, 4, 5), 3) shouldBe List(4, 5, 1, 2, 3)
21+
rotate(Array(1, 2, 3, 4, 5), 4) shouldBe List(5, 1, 2, 3, 4)
22+
rotate(Array(1, 2, 3, 4, 5), 5) shouldBe List(1, 2, 3, 4, 5)
23+
rotate(Array(1, 2, 3, 4, 5), 6) shouldBe List(2, 3, 4, 5, 1)
24+
}
25+
}

0 commit comments

Comments
 (0)