Skip to content

Commit c1a0c81

Browse files
committed
GP-39 fun-prime-numbers
* refactor main to introduce 3 more methods and corresponding tests
1 parent fdcbdfc commit c1a0c81

File tree

2 files changed

+101
-7
lines changed

2 files changed

+101
-7
lines changed

5-0-functional-programming/5-4-1-fun-prime-numbers/src/main/java/com/bobocode/fp/PrimeNumbers.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.bobocode.util.ExerciseNotCompletedException;
44

55
import java.util.List;
6+
import java.util.Map;
67
import java.util.function.IntConsumer;
8+
import java.util.stream.IntStream;
79

810
/**
911
* {@link PrimeNumbers} provides an API to work with prime numbers. The implementation is based on the
@@ -15,6 +17,26 @@ public class PrimeNumbers {
1517
private PrimeNumbers() {
1618
}
1719

20+
/**
21+
* Generates an infinite int stream of prime numbers.
22+
* The stream values are 2, 3, 5,... and so on.
23+
*
24+
* @return an infinite int stream of prime numbers
25+
*/
26+
public static IntStream stream() {
27+
throw new ExerciseNotCompletedException(); // todo: create an infinite stream of ints, then filter prime numbs
28+
}
29+
30+
/**
31+
* Generates an int stream of a certain amount of prime numbers.
32+
* It is based on the {@link PrimeNumbers#stream()} but specifies the exact size of the stream.
33+
*
34+
* @return an int stream of prime numbers with a specified size
35+
*/
36+
public static IntStream stream(int size) {
37+
throw new ExerciseNotCompletedException(); // todo: use the prev to generate a stream method but limit its size
38+
}
39+
1840
/**
1941
* Calculates the sum on first n prime numbers.
2042
* E.g. if n = 5, the result should be 2 + 3 + 5 + 7 + 11 = 28
@@ -23,7 +45,7 @@ private PrimeNumbers() {
2345
* @return the sum of n prime numbers
2446
*/
2547
public static int sum(int n) {
26-
throw new ExerciseNotCompletedException(); // todo: create an infinite stream of ints, then filter prime numbs
48+
throw new ExerciseNotCompletedException(); // todo: use prev method and calculate the sum
2749

2850
}
2951

@@ -32,8 +54,8 @@ public static int sum(int n) {
3254
*
3355
* @return a list of collected prime numbers
3456
*/
35-
public static List<Integer> collect(int n) {
36-
throw new ExerciseNotCompletedException(); // todo: reuse the logic of prime numbers stream and collect them
57+
public static List<Integer> list(int n) {
58+
throw new ExerciseNotCompletedException(); // todo: collect prime numbers into the list
3759
}
3860

3961
/**
@@ -43,6 +65,20 @@ public static List<Integer> collect(int n) {
4365
* @param consumer a logic that should be applied to the found prime number
4466
*/
4567
public static void processByIndex(int idx, IntConsumer consumer) {
46-
throw new ExerciseNotCompletedException(); // todo: reuse the logic of prime numbers stream then process the last one
68+
throw new ExerciseNotCompletedException(); // todo: find an element in the stream by index and process it
69+
}
70+
71+
/**
72+
* Creates a list of n prime numbers and returns a map where all of those prime numbers are groped. The key represents
73+
* an amount of digits and the value is a corresponding list of all prime numbers.
74+
* <p>
75+
* So if you will call this method for with argument 20, you will receive the following map:
76+
* {1=[2, 3, 5, 7], 2=[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]}
77+
*
78+
* @param n – the amount of prime numbers
79+
* @return a map with prime number grouped by the amount of digits
80+
*/
81+
public static Map<Integer, List<Integer>> groupByAmountOfDigits(int n) {
82+
throw new ExerciseNotCompletedException(); // todo: group n prime numbers by the amount of digits
4783
}
4884
}

5-0-functional-programming/5-4-1-fun-prime-numbers/src/test/java/com/bobocode/fp/PrimeNumbersTest.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.jupiter.api.MethodOrderer;
44
import org.junit.jupiter.api.Order;
5+
import org.junit.jupiter.api.Test;
56
import org.junit.jupiter.api.TestMethodOrder;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.provider.Arguments;
@@ -10,15 +11,42 @@
1011

1112
import java.util.ArrayList;
1213
import java.util.List;
14+
import java.util.Map;
1315
import java.util.stream.Stream;
1416

17+
import static java.util.stream.Collectors.toList;
1518
import static org.assertj.core.api.Assertions.assertThat;
1619
import static org.junit.jupiter.params.provider.Arguments.arguments;
1720

1821
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1922
class PrimeNumbersTest {
2023

2124
@Order(1)
25+
@Test
26+
void stream() {
27+
var primeNumbersStream = PrimeNumbers.stream();
28+
29+
var primeList = primeNumbersStream
30+
.limit(5)
31+
.boxed()
32+
.collect(toList());
33+
34+
assertThat(primeList).isEqualTo(List.of(2, 3, 5, 7, 11));
35+
}
36+
37+
@Order(2)
38+
@Test
39+
void streamN() {
40+
var primeNumbersStream = PrimeNumbers.stream(8);
41+
42+
var primeList = primeNumbersStream
43+
.boxed()
44+
.collect(toList());
45+
46+
assertThat(primeList).isEqualTo(List.of(2, 3, 5, 7, 11, 13, 17, 19));
47+
}
48+
49+
@Order(3)
2250
@ParameterizedTest
2351
@CsvSource({"0, 0", "1, 2", "2, 5", "3, 10", "4, 17", "5, 28", "10, 129", "20, 639"})
2452
void sum(int n, int sumOfPrimes) {
@@ -27,16 +55,16 @@ void sum(int n, int sumOfPrimes) {
2755
assertThat(result).isEqualTo(sumOfPrimes);
2856
}
2957

30-
@Order(2)
58+
@Order(4)
3159
@ParameterizedTest
3260
@MethodSource("collectArgs")
3361
void collect(int n, List<Integer> primeNumbersList) {
34-
List<Integer> result = PrimeNumbers.collect(n);
62+
List<Integer> result = PrimeNumbers.list(n);
3563

3664
assertThat(result).isEqualTo(primeNumbersList);
3765
}
3866

39-
@Order(3)
67+
@Order(5)
4068
@ParameterizedTest
4169
@CsvSource({"0, 2", "1, 3", "2, 5", "3, 7", "10, 31", "20, 73", "279, 1811"})
4270
void processByIndexFindsCorrectPrimeNumber(int index, int primeNumber) {
@@ -57,4 +85,34 @@ static Stream<Arguments> collectArgs() {
5785
arguments(10, List.of(2, 3, 5, 7, 11, 13, 17, 19, 23, 29))
5886
);
5987
}
88+
89+
@Order(6)
90+
@ParameterizedTest
91+
@MethodSource("groupByAmountOfDigitsArgs")
92+
void groupByAmountOfDigits(int count, Map<Integer, List<Integer>> digitsToPrimesMap) {
93+
var result = PrimeNumbers.groupByAmountOfDigits(count);
94+
95+
assertThat(result).isEqualTo(digitsToPrimesMap);
96+
}
97+
98+
static Stream<Arguments> groupByAmountOfDigitsArgs() {
99+
return Stream.of(
100+
arguments(1, Map.of(1, List.of(2))),
101+
arguments(10, Map.of(
102+
1, List.of(2, 3, 5, 7),
103+
2, List.of(11, 13, 17, 19, 23, 29)
104+
)),
105+
arguments(20, Map.of(
106+
1, List.of(2, 3, 5, 7),
107+
2, List.of(11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71)
108+
)),
109+
arguments(30, Map.of(
110+
1, List.of(2, 3, 5, 7),
111+
2, List.of(11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97),
112+
3, List.of(101, 103, 107, 109, 113)
113+
)
114+
)
115+
116+
);
117+
}
60118
}

0 commit comments

Comments
 (0)