Skip to content

Develop #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions algorithm-exercises-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,76 +16,62 @@


@TestInstance(Lifecycle.PER_CLASS)
class ArraysLeftRotationTest {
class ArraysLeftRotationTest {

public class ArraysLeftRotationTestCase {
public List<Integer> input;
public List<Integer> expected;
public JsonNode testCases;

public ArraysLeftRotationTestCase(List<Integer> input, List<Integer> 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<Integer> input;
public Integer d;
public List<Integer> expected;

public ArraysLeftRotationsTestCase(List<Integer> input, Integer d, List<Integer> expected) {
this.input = input;
this.d = d;
this.expected = expected;
}
}
@Test void testRotLeftOne() throws JsonProcessingException {

public List<ArraysLeftRotationTestCase> testCases;
public List<ArraysLeftRotationsTestCase> 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<Integer> tlist = Arrays.stream(input).boxed().toList();
List<Integer> 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<Integer> texpected = Arrays.stream(expected).boxed().toList();

@Test void testRotLeftOne() {
for (ArraysLeftRotationTestCase testCase : this.testCases) {
List<Integer> 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<Integer> 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<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expected = Arrays.asList(5, 1, 2, 3, 4);
Integer d = 4;

List<Integer> solutionFound = ArraysLeftRotation.rotLeft(input, d);

assertEquals(expected, solutionFound,
String.format("%s(%s, %d) answer must be: %s",
"CompareTriplets.compareTriplets",
input.toString(),
d,
expected.toString())
);
}

}
Original file line number Diff line number Diff line change
@@ -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]}
]