-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondPartTasks.java
More file actions
51 lines (44 loc) · 2.79 KB
/
SecondPartTasks.java
File metadata and controls
51 lines (44 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package sp;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.*;
public final class SecondPartTasks {
private SecondPartTasks() {
}
// Найти строки из переданных файлов, в которых встречается указанная подстрока.
public static List<String> findQuotes(List<String> paths, CharSequence sequence) {
return paths.stream().flatMap(path -> {
try (Stream<String> stream = Files.lines(Paths.get(path))) {
return stream.filter(str -> str.contains(sequence)).collect(Collectors.toList()).stream();
} catch (IOException e) {
return Stream.of();
}
}).collect(Collectors.toList());
}
// В квадрат с длиной стороны 1 вписана мишень.
// Стрелок атакует мишень и каждый раз попадает в произвольную точку квадрата.
// Надо промоделировать этот процесс с помощью класса java.util.Random и посчитать, какова вероятность попасть в мишень.
public static double piDividedBy4() {
int numberOfTries = 1000000;
double[] doubles = (new Random()).doubles(-0.5, 0.5).limit(numberOfTries).toArray();
return (double) IntStream.range(0, numberOfTries / 2).filter(i -> Math.pow(doubles[2 * i], 2)
+ Math.pow(doubles[2 * i + 1], 2) < 0.25).count() * 2 / numberOfTries;
}
// Дано отображение из имени автора в список с содержанием его произведений.
// Надо вычислить, чья общая длина произведений наибольшая.
public static String findPrinter(Map<String, List<String>> compositions) {
return compositions.entrySet().stream().max(Comparator.comparing(entry -> entry.getValue().stream()
.mapToInt(String::length).sum())).map(Map.Entry::getKey).orElse(null);
}
// Вы крупный поставщик продуктов. Каждая торговая сеть делает вам заказ в виде Map<Товар, Количество>.
// Необходимо вычислить, какой товар и в каком количестве надо поставить.
public static Map<String, Integer> calculateGlobalOrder(List<Map<String, Integer>> orders) {
return orders.stream().flatMap(order -> order.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a + b));
}
}