Provides custom, modern class for 1D Array task
This commit is contained in:
parent
4d6be24f49
commit
540a63dbc2
3 changed files with 67 additions and 17 deletions
|
@ -0,0 +1,26 @@
|
|||
package eu.ztsh.training.hackerrank.datastructures.array_1d;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Custom modern class for 1D Array task.
|
||||
*/
|
||||
public class CustomSolution {
|
||||
|
||||
public static void main(String[] args) {
|
||||
var scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
// 1. Create an array, a, capable of holding n integers.
|
||||
int[] a = new int[n];
|
||||
var index = new AtomicInteger(0);
|
||||
// 2. Modify the code in the loop so that it saves each sequential value to its corresponding location in the array.
|
||||
Stream.generate(scan::nextInt)
|
||||
.limit(n) // takeWhile doesn't fill last element
|
||||
.forEachOrdered(item -> a[index.getAndIncrement()] = item);
|
||||
Arrays.stream(a).forEachOrdered(System.out::println);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,18 +2,24 @@ package eu.ztsh.training.hackerrank.datastructures.array_1d;
|
|||
|
||||
import java.util.Scanner;
|
||||
|
||||
// https://www.hackerrank.com/challenges/java-1d-array-introduction/problem
|
||||
/**
|
||||
* Java 1D Array task.
|
||||
* Task spec: Java 8 + partially locked editor
|
||||
* source: https://www.hackerrank.com/challenges/java-1d-array-introduction/problem
|
||||
*/
|
||||
public class Solution {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
// BEGIN: unlocked editor
|
||||
int[] a = new int[n];
|
||||
int index = 0;
|
||||
while (scan.hasNext()) {
|
||||
a[index] = scan.nextInt();
|
||||
index++;
|
||||
}
|
||||
// END: unlocked editor
|
||||
scan.close();
|
||||
// Prints each sequential element in array a
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
|
|
|
@ -4,30 +4,48 @@ import java.util.List;
|
|||
import eu.ztsh.training.hackerrank.HackerRankTest;
|
||||
import eu.ztsh.training.hackerrank.SolutionClassDescription;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@DisplayName("HackerRank challenges: Java 1D Array")
|
||||
class SolutionTest extends HackerRankTest {
|
||||
class SolutionTest {
|
||||
|
||||
@Test
|
||||
public void testCase0() {
|
||||
simpleAssert(
|
||||
List.of("5", "10", "20", "30", "40", "50"),
|
||||
List.of("10", "20", "30", "40", "50")
|
||||
);
|
||||
@Nested
|
||||
@DisplayName("Original Solution class")
|
||||
class OriginalSolutionTest extends Array1DTest {
|
||||
@Override
|
||||
protected SolutionClassDescription getSolutionClassDescription() {
|
||||
return new SolutionClassDescription(Solution.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase1() {
|
||||
simpleAssert(
|
||||
List.of("3", "100", "200", "100"),
|
||||
List.of("100", "200", "100")
|
||||
);
|
||||
@Nested
|
||||
@DisplayName("Custom Solution class")
|
||||
class CustomSolutionTest extends Array1DTest {
|
||||
@Override
|
||||
protected SolutionClassDescription getSolutionClassDescription() {
|
||||
return new SolutionClassDescription(CustomSolution.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SolutionClassDescription getSolutionClassDescription() {
|
||||
return new SolutionClassDescription(Solution.class);
|
||||
abstract static class Array1DTest extends HackerRankTest {
|
||||
|
||||
@Test
|
||||
public void testCase0() {
|
||||
simpleAssert(
|
||||
List.of("5", "10", "20", "30", "40", "50"),
|
||||
List.of("10", "20", "30", "40", "50")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase1() {
|
||||
simpleAssert(
|
||||
List.of("3", "100", "200", "100"),
|
||||
List.of("100", "200", "100")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue