From ad3ac263c5d47e642b61988c1cd3b92c1f6b87a9 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Sun, 1 Sep 2024 14:35:42 -0400 Subject: [PATCH 1/2] [CONFIG] gradle dependencies: jackson (JSON) parser added. --- algorithm-exercises-java/build.gradle | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/algorithm-exercises-java/build.gradle b/algorithm-exercises-java/build.gradle index 8406e85..bfbabc6 100644 --- a/algorithm-exercises-java/build.gradle +++ b/algorithm-exercises-java/build.gradle @@ -29,6 +29,12 @@ dependencies { // This dependency is used by the application. implementation 'com.google.guava:guava:33.3.0-jre' + + // + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.17.2' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.17.2' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.17.2' + } application { From 9f9a2b01c60a41dd5b0d109f1b761aecf08e62e5 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Sun, 1 Sep 2024 14:53:38 -0400 Subject: [PATCH 2/2] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. Test cases loaded from JSON. --- .../arrays/ArraysLeftRotationTest.java | 101 ++++++++---------- .../ctci_array_left_rotation.testcases.json | 7 ++ 2 files changed, 53 insertions(+), 55 deletions(-) create mode 100644 algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java index 52f7bb2..945f2b8 100644 --- a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java @@ -2,6 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.BeforeAll; @@ -11,76 +16,62 @@ @TestInstance(Lifecycle.PER_CLASS) - class ArraysLeftRotationTest { +class ArraysLeftRotationTest { - public class ArraysLeftRotationTestCase { - public List input; - public List expected; + public JsonNode testCases; - public ArraysLeftRotationTestCase(List input, List expected) { - this.input = input; - this.expected = expected; - } + @BeforeAll + public void setup() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + + String path = String.join("/", "hackerrank", + "interview_preparation_kit", + "arrays", + "ctci_array_left_rotation.testcases.json"); + File file = new File( + this.getClass() + .getClassLoader() + .getResource(path) + .getFile() + ); + this.testCases = objectMapper.readTree(file); } - public class ArraysLeftRotationsTestCase { - public List input; - public Integer d; - public List expected; - - public ArraysLeftRotationsTestCase(List input, Integer d, List expected) { - this.input = input; - this.d = d; - this.expected = expected; - } - } + @Test void testRotLeftOne() throws JsonProcessingException { - public List testCases; - public List testRotationsCases; + ObjectMapper mapper = new ObjectMapper(); - @BeforeAll - public void setup() { - this.testCases = Arrays.asList( - new ArraysLeftRotationTestCase(Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(2, 3, 4, 5, 1)), - new ArraysLeftRotationTestCase(Arrays.asList(2, 3, 4, 5, 1), Arrays.asList(3, 4, 5, 1, 2)), - new ArraysLeftRotationTestCase(Arrays.asList(3, 4, 5, 1, 2), Arrays.asList(4, 5, 1, 2, 3)), - new ArraysLeftRotationTestCase(Arrays.asList(4, 5, 1, 2, 3), Arrays.asList(5, 1, 2, 3, 4)), - new ArraysLeftRotationTestCase(Arrays.asList(5, 1, 2, 3, 4), Arrays.asList(1, 2, 3, 4, 5)) - ); + for (JsonNode testCase : this.testCases) { + int[] input = mapper.readValue(testCase.get("input").toString(), int[].class); + List tlist = Arrays.stream(input).boxed().toList(); + List solutionFound = ArraysLeftRotation.rotLeftOne(tlist); - this.testRotationsCases = Arrays.asList( - new ArraysLeftRotationsTestCase( - Arrays.asList(1, 2, 3, 4, 5), - 4, - Arrays.asList(5, 1, 2, 3, 4)) - ); - } + int[] expected = mapper.readValue(testCase.get("expected").toString(), int[].class); + List texpected = Arrays.stream(expected).boxed().toList(); - @Test void testRotLeftOne() { - for (ArraysLeftRotationTestCase testCase : this.testCases) { - List solutionFound = ArraysLeftRotation.rotLeftOne(testCase.input); - assertEquals(testCase.expected, solutionFound, + assertEquals(texpected, solutionFound, String.format("%s(%s) answer must be: %s", "CompareTriplets.compareTriplets", - testCase.input.toString(), - testCase.expected.toString()) + testCase.get("input"), + testCase.get("expected")) ); } } @Test void testRotLeft() { - for (ArraysLeftRotationsTestCase testCase : this.testRotationsCases) { - List solutionFound = ArraysLeftRotation.rotLeft(testCase.input, testCase.d); - - assertEquals(testCase.expected, solutionFound, - String.format("%s(%s, %d) answer must be: %s", - "CompareTriplets.compareTriplets", - testCase.input.toString(), - testCase.d, - testCase.expected.toString()) - ); - } + List input = Arrays.asList(1, 2, 3, 4, 5); + List expected = Arrays.asList(5, 1, 2, 3, 4); + Integer d = 4; + + List solutionFound = ArraysLeftRotation.rotLeft(input, d); + + assertEquals(expected, solutionFound, + String.format("%s(%s, %d) answer must be: %s", + "CompareTriplets.compareTriplets", + input.toString(), + d, + expected.toString()) + ); } - } diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json new file mode 100644 index 0000000..1750d96 --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json @@ -0,0 +1,7 @@ +[ + {"input": [1, 2, 3, 4, 5], "expected": [2, 3, 4, 5, 1]}, + {"input": [2, 3, 4, 5, 1], "expected": [3, 4, 5, 1, 2]}, + {"input": [3, 4, 5, 1, 2], "expected": [4, 5, 1, 2, 3]}, + {"input": [4, 5, 1, 2, 3], "expected": [5, 1, 2, 3, 4]}, + {"input": [5, 1, 2, 3, 4], "expected": [1, 2, 3, 4, 5]} +]