Skip to content
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

V0.2.1 #2

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Apply you knowledge of _streams_, _lambdas_, _method handlers_ and other functio

There are also some tasks that will make think even experienced programmers. Try to solve them all.

> **25** tasks are available now
> **> 30** tasks are available now

### What to do

Expand Down
73 changes: 73 additions & 0 deletions src/tasks/StreamTasks.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tasks;
import java.util.Collection;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
Expand Down Expand Up @@ -250,4 +251,76 @@ public IntStream task25 (List <Integer> numbers1, List <Integer> numbers2) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

/**
* @return Stream of prefixes with length that equals to the index of prefix + 1
* @see List#subList(int, int)
* @example [0, 1, 2, 3, 4] -> [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
* @lines 1
*/
public Stream <List <Integer>> task26 (List <Integer> numbers) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

/**
* @return Stream of groups of values with length that is not more than given `sizeLimit`
* @see List#subList(int, int)
* @example [0, 1, 2, 3, 4, 5, 6, 7], 3 -> [[0, 1, 2], [3, 4, 5], [6, 7]]
* @lines 4-
*/
public <T> Stream <List <T>> task27 (List <T> values, int sizeLimit) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

/**
* @return Stream of integer function roots from segment [-1000, 1000] in ascending order
* @see IntStream#filter(IntPredicate)
* @example x^2 - 3x - 10 -> [-2, 5]
* @lines 1
*/
public IntStream task28 (Function <Integer, Integer> function) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

/**
* @return Average value of given numbers (or -1 if stream is empty)
* @see Stream#mapToInt(ToIntFunction)
* @see IntStream#average()
* @lines 1
*/
public double task29 (List <Integer> numbers) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

/**
* @return Values variation (the difference between maximal and minimal values)
* @see IntStream#summaryStatistics()
* @see IntSummaryStatistics
* @lines 2-
*/
public int task30 (List <Integer> numbers) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

// MULTI-DIMENTION //

/**
* @return Stream of all values from given 2-dim list
* @see Stream#flatMap(Function)
* @see List#stream()
* @example [[1, 2], [4, 5, 3], [], [7]] -> [1, 2, 4, 5, 3, 7]
* @lines 1
*/
public <T> Stream <T> task31 (List <List <T>> numbers) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

/**
* @return Stream of sums of values in internal lists
* @example [[1, 2], [4, 5, 3], [], [7]] -> [3, 12, 0, 7]
* @lines 1
*/
public Stream <Integer> task32 (List <List <Integer>> numbers) {
throw new UnsupportedOperationException ("Implement method instead of this line");
}

}
3 changes: 3 additions & 0 deletions src/tasks/StreamTasksMain.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package tasks;
import java.io.PrintStream;
import java.util.Locale;

import tasks.solution.StreamTasksSolution;
Expand All @@ -11,6 +12,8 @@ public class StreamTasksMain {
/* DO NOT TOUCH METHODS BELLOW */
/*******************************/

public static PrintStream ORIGINAL_OUT = System.out;

public static void main (String ... args) {
Locale.setDefault (Locale.ENGLISH);

Expand Down
39 changes: 39 additions & 0 deletions src/tasks/solution/StreamTasksSolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,43 @@ public IntStream task25 (List <Integer> numbers1, List <Integer> numbers2) {
. map (i -> numbers1.get (i) + numbers2.get (i));
}

@Override
public Stream <List <Integer>> task26 (List <Integer> numbers) {
return IntStream.range (0, numbers.size ()).mapToObj (i -> numbers.subList (0, i + 1));
}

@Override
public <T> Stream <List <T>> task27 (List <T> values, int sl) {
final var len = values.size ();
return IntStream.range (0, (int) Math.ceil (len * 1.0 / sl)).mapToObj (
i -> values.subList (i * sl, Math.min ((i + 1) * sl, len)
));
}

@Override
public IntStream task28 (Function <Integer, Integer> function) {
return IntStream.rangeClosed (-1000, 1000).filter (i -> function.apply (i) == 0);
}

@Override
public double task29 (List <Integer> numbers) {
return numbers.stream ().mapToInt (i -> i).average ().orElse (0);
}

@Override
public int task30 (List <Integer> numbers) {
final var statistics = numbers.stream ().mapToInt (i -> i).summaryStatistics ();
return statistics.getMax () - statistics.getMin ();
}

@Override
public <T> Stream <T> task31 (List <List <T>> numbers) {
return numbers.stream ().flatMap (List::stream);
}

@Override
public Stream <Integer> task32 (List <List <Integer>> numbers) {
return numbers.stream ().map (list -> list.stream ().mapToInt (i -> i).sum ());
}

}
17 changes: 16 additions & 1 deletion src/tests/InvocationResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class InvocationResult {
private long runtime;

private final List <Object> consumers = new ArrayList <> ();
private final List <String> output = new ArrayList <> ();

public InvocationResult (Object result, long runtime, List <Object> consumerResults) {
this.result = result; this.runtime = runtime;
Expand All @@ -30,6 +31,16 @@ public InvocationResult addConsumerValues (List <Object> values) {
return this;
}

public InvocationResult addOutput (String output) {
this.output.add (0, output);
return this;
}

public InvocationResult addOutputs (List <String> output) {
this.output.addAll (0, output);
return this;
}

public InvocationResult addRuntime (long delta) {
runtime += delta;
return this;
Expand All @@ -38,16 +49,20 @@ public InvocationResult addRuntime (long delta) {
public InvocationResult addAnotherResult (InvocationResult result) {
addConsumerValues (result.getConsumers ());
addRuntime (result.getRuntime ());
addOutputs (result.getOutput ());
return this;
}

public long getRuntime () {
return runtime;
}


public List <Object> getConsumers () {
return Collections.unmodifiableList (consumers);
}

public List <String> getOutput () {
return Collections.unmodifiableList (output);
}

}
7 changes: 7 additions & 0 deletions src/tests/OutputAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public static void assertOutput (Integer actual, Integer expected) {
);
}

public static void assertOutput (Double actual, Double expected) {
assert Objects.equals (actual, expected) : String.format (
"Double value `%d` was expected (actual given: %d)",
expected, actual
);
}

public static <T> void assertOutput (Stream <T> actual, boolean parallel, List <T> expected) {
assert actual.isParallel () == parallel : String.format (
"Stream should%s be parallel", parallel ? "" : " not"
Expand Down
58 changes: 58 additions & 0 deletions src/tests/StreamTasksTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import tests.inputs.LevelsArrange;
import tests.presets.HexStrNumbers;
import tests.presets.IntNumbers;
import tests.presets.IntStrNumbers;
Expand Down Expand Up @@ -228,5 +229,62 @@ public abstract IntStream task25 (
percentage = {1.3, 1.4}, variation = 10)
List <Integer> numbers2
);

@Test (order = 2)
@TestResult (repeat = 1, wrap = List.class)
public abstract Stream <List <Integer>> task26 (
@TestInputCollection (presets = {IntNumbers.class}, percentage = {0.4, 0.5, 1.2, 1.7, 2.0}, variation = 20)
List <Integer> numbers
);

@Test (order = 2)
@TestResult (repeat = 1, wrap = List.class)
public abstract <T> Stream <List <T>> task27 (
@TestInputCollection (presets = {IntNumbers.class, IntStrNumbers.class},
percentage = {0.3, 0.6, 1.1, 1.8, 1.9, 2.4}, variation = 20)
List <T> values,
@TestInputConstant (sequence = {0}, parameter = 4, variation = 200)
int sizeLimit
);

@Test (order = 2)
@TestResult (repeat = 1, wrap = List.class)
public abstract IntStream task28 (
@TestInputFunction (indices = {5, 6})
Function <Integer, Integer> function
);

@Test (order = 2)
@TestResult (repeat = 1)
public abstract double task29 (
@TestInputCollection (presets = {IntNumbers.class}, constant = {0, 0, 0, 0},
percentage = {0.1, 0.5, 0.8, 1.4, 1.7}, variation = 10)
List <Integer> numbers
);

@Test (order = 3)
@TestResult (repeat = 1)
public abstract int task30 (
@TestInputCollection (presets = {IntNumbers.class}, constant = {0, 0, 0, 0},
percentage = {0.1, 0.5, 0.8, 1.4, 1.7}, variation = 10)
List <Integer> numbers
);

@Test (order = 3)
@TestResult (repeat = 1, wrap = List.class)
public abstract <T> Stream <T> task31 (
@TestInputCollection (presets = {IntNumbers.class, HexStrNumbers.class, Names.class},
levels = 2, arrengement = LevelsArrange.DISHONEST, percentage = {0.1, 0.2, 0.3, 1.1, 1.2},
variation = 20)
List <List <T>> numbers
);

@Test (order = 3)
@TestResult (repeat = 1, wrap = List.class)
public abstract Stream <Integer> task32 (
@TestInputCollection (presets = {IntNumbers.class}, levels = 2, arrengement = LevelsArrange.DISHONEST,
percentage = {0.1, 0.2, 0.3, 1.1, 1.2}, variation = 20)
List <List <Integer>> numbers
);

}
11 changes: 10 additions & 1 deletion src/tests/TaskTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ public List <?> getInputs () {
public InvocationResult runTests (StreamTasksTests implementation, StreamTasksTests reference) {
final var stub = new InvocationResult (null, 0);
return cases.stream ().map (caze -> caze.apply (implementation, reference))
. reduce (stub, (a, b) -> a == stub && b != null ? b : a);
. reduce (stub, (a, b) -> {
if (a == stub && b != null) {
return b;
} else if (a != stub && b != null) {
b.addAnotherResult (a);
return b;
} else {
return a;
}
});
}

}
Loading