Skip to content

Commit aabab82

Browse files
authored
Merge pull request #212 from sir-gon/develop
Develop
2 parents 456bb68 + 9f9a2b0 commit aabab82

File tree

3 files changed

+59
-55
lines changed

3 files changed

+59
-55
lines changed

algorithm-exercises-java/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ dependencies {
2929

3030
// This dependency is used by the application.
3131
implementation 'com.google.guava:guava:33.3.0-jre'
32+
33+
//
34+
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.17.2'
35+
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.17.2'
36+
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.17.2'
37+
3238
}
3339

3440
application {

algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.io.File;
9+
import java.io.IOException;
510
import java.util.Arrays;
611
import java.util.List;
712
import org.junit.jupiter.api.BeforeAll;
@@ -11,76 +16,62 @@
1116

1217

1318
@TestInstance(Lifecycle.PER_CLASS)
14-
class ArraysLeftRotationTest {
19+
class ArraysLeftRotationTest {
1520

16-
public class ArraysLeftRotationTestCase {
17-
public List<Integer> input;
18-
public List<Integer> expected;
21+
public JsonNode testCases;
1922

20-
public ArraysLeftRotationTestCase(List<Integer> input, List<Integer> expected) {
21-
this.input = input;
22-
this.expected = expected;
23-
}
23+
@BeforeAll
24+
public void setup() throws IOException {
25+
ObjectMapper objectMapper = new ObjectMapper();
26+
27+
String path = String.join("/", "hackerrank",
28+
"interview_preparation_kit",
29+
"arrays",
30+
"ctci_array_left_rotation.testcases.json");
31+
File file = new File(
32+
this.getClass()
33+
.getClassLoader()
34+
.getResource(path)
35+
.getFile()
36+
);
37+
this.testCases = objectMapper.readTree(file);
2438
}
2539

26-
public class ArraysLeftRotationsTestCase {
27-
public List<Integer> input;
28-
public Integer d;
29-
public List<Integer> expected;
30-
31-
public ArraysLeftRotationsTestCase(List<Integer> input, Integer d, List<Integer> expected) {
32-
this.input = input;
33-
this.d = d;
34-
this.expected = expected;
35-
}
36-
}
40+
@Test void testRotLeftOne() throws JsonProcessingException {
3741

38-
public List<ArraysLeftRotationTestCase> testCases;
39-
public List<ArraysLeftRotationsTestCase> testRotationsCases;
42+
ObjectMapper mapper = new ObjectMapper();
4043

41-
@BeforeAll
42-
public void setup() {
43-
this.testCases = Arrays.asList(
44-
new ArraysLeftRotationTestCase(Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(2, 3, 4, 5, 1)),
45-
new ArraysLeftRotationTestCase(Arrays.asList(2, 3, 4, 5, 1), Arrays.asList(3, 4, 5, 1, 2)),
46-
new ArraysLeftRotationTestCase(Arrays.asList(3, 4, 5, 1, 2), Arrays.asList(4, 5, 1, 2, 3)),
47-
new ArraysLeftRotationTestCase(Arrays.asList(4, 5, 1, 2, 3), Arrays.asList(5, 1, 2, 3, 4)),
48-
new ArraysLeftRotationTestCase(Arrays.asList(5, 1, 2, 3, 4), Arrays.asList(1, 2, 3, 4, 5))
49-
);
44+
for (JsonNode testCase : this.testCases) {
45+
int[] input = mapper.readValue(testCase.get("input").toString(), int[].class);
46+
List<Integer> tlist = Arrays.stream(input).boxed().toList();
47+
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(tlist);
5048

51-
this.testRotationsCases = Arrays.asList(
52-
new ArraysLeftRotationsTestCase(
53-
Arrays.asList(1, 2, 3, 4, 5),
54-
4,
55-
Arrays.asList(5, 1, 2, 3, 4))
56-
);
57-
}
49+
int[] expected = mapper.readValue(testCase.get("expected").toString(), int[].class);
50+
List<Integer> texpected = Arrays.stream(expected).boxed().toList();
5851

59-
@Test void testRotLeftOne() {
60-
for (ArraysLeftRotationTestCase testCase : this.testCases) {
61-
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(testCase.input);
6252

63-
assertEquals(testCase.expected, solutionFound,
53+
assertEquals(texpected, solutionFound,
6454
String.format("%s(%s) answer must be: %s",
6555
"CompareTriplets.compareTriplets",
66-
testCase.input.toString(),
67-
testCase.expected.toString())
56+
testCase.get("input"),
57+
testCase.get("expected"))
6858
);
6959
}
7060
}
7161

7262
@Test void testRotLeft() {
73-
for (ArraysLeftRotationsTestCase testCase : this.testRotationsCases) {
74-
List<Integer> solutionFound = ArraysLeftRotation.rotLeft(testCase.input, testCase.d);
75-
76-
assertEquals(testCase.expected, solutionFound,
77-
String.format("%s(%s, %d) answer must be: %s",
78-
"CompareTriplets.compareTriplets",
79-
testCase.input.toString(),
80-
testCase.d,
81-
testCase.expected.toString())
82-
);
83-
}
63+
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
64+
List<Integer> expected = Arrays.asList(5, 1, 2, 3, 4);
65+
Integer d = 4;
66+
67+
List<Integer> solutionFound = ArraysLeftRotation.rotLeft(input, d);
68+
69+
assertEquals(expected, solutionFound,
70+
String.format("%s(%s, %d) answer must be: %s",
71+
"CompareTriplets.compareTriplets",
72+
input.toString(),
73+
d,
74+
expected.toString())
75+
);
8476
}
85-
8677
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{"input": [1, 2, 3, 4, 5], "expected": [2, 3, 4, 5, 1]},
3+
{"input": [2, 3, 4, 5, 1], "expected": [3, 4, 5, 1, 2]},
4+
{"input": [3, 4, 5, 1, 2], "expected": [4, 5, 1, 2, 3]},
5+
{"input": [4, 5, 1, 2, 3], "expected": [5, 1, 2, 3, 4]},
6+
{"input": [5, 1, 2, 3, 4], "expected": [1, 2, 3, 4, 5]}
7+
]

0 commit comments

Comments
 (0)