Skip to content

Instantly share code, notes, and snippets.

@maciekmm
Created March 8, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maciekmm/d313953b642a841720c606bd9e866e7e to your computer and use it in GitHub Desktop.
Save maciekmm/d313953b642a841720c606bd9e866e7e to your computer and use it in GitHub Desktop.
Zbiór zadań: 73, 79
73.1 Słów z dwoma kolejnymi takimi samymi literami:
204
73.2 Statystyka:
A: 632 (7.55 %)
B: 196 (2.34 %)
C: 162 (1.94 %)
D: 422 (5.04 %)
E: 1093 (13.06 %)
F: 213 (2.55 %)
G: 151 (1.80 %)
H: 566 (6.76 %)
I: 522 (6.24 %)
J: 2 (0.02 %)
K: 64 (0.76 %)
L: 402 (4.80 %)
M: 193 (2.31 %)
N: 557 (6.66 %)
O: 641 (7.66 %)
P: 93 (1.11 %)
Q: 6 (0.07 %)
R: 524 (6.26 %)
S: 485 (5.80 %)
T: 792 (9.46 %)
U: 185 (2.21 %)
V: 84 (1.00 %)
W: 196 (2.34 %)
X: 3 (0.04 %)
Y: 185 (2.21 %)
Z: 0 (0.00 %)
73.3 Najdłuższe podsłowa:
Długość najdłuższego podsłowa:
4
Słów z taką długością podsłowa:
6
Pierwsze ze słów:
FRIENDSHIP
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public class Main {
private static final Pattern PATTERN = Pattern.compile("(.)\\1");
private static final Pattern SUBWORD = Pattern.compile("[^AEIOUY]+");
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("/mnt/development/projects/java/private/matura/73/src/tekst.txt"));
ArrayList<String> words = new ArrayList<>();
while (scanner.hasNext()) {
words.add(scanner.next());
}
System.out.println("73.1 Słów z dwoma kolejnymi takimi samymi literami: ");
System.out.println(words.stream().filter((s) -> PATTERN.matcher(s).find()).count());
System.out.println("\n73.2 Statystyka: ");
HashMap<Character, Integer> stats = new HashMap<>();
words.stream().flatMap(s -> s.chars().boxed()).forEach((c) -> stats.put((char) c.byteValue(), stats.getOrDefault((char) c.byteValue(), 0) + 1));
long total = words.stream().flatMap(s -> s.chars().boxed()).count();
for (char i = 'A'; i <= 'Z'; i++) {
System.out.printf("%s: %d (%.2f %%)\n", i, stats.getOrDefault(i, 0), ((stats.getOrDefault(i, 0) * 100.0) / total));
}
System.out.println("\n73.3 Najdłuższe podsłowa: ");
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
int longestSubword = words.stream().flatMap(s -> {
Matcher matcher = SUBWORD.matcher(s);
Stream.Builder<String> stream = Stream.builder();
int longest = 0;
while (matcher.find()) {
stream.add(matcher.group());
if (matcher.group().length() > longest) {
longest = matcher.group().length();
}
}
if (longest != 0) {
map.put(s, longest);
}
return stream.build();
}).max(Comparator.comparingInt(String::length)).get().length();
System.out.println("Długość najdłuższego podsłowa: ");
System.out.println(longestSubword);
System.out.println("Słów z taką długością podsłowa: ");
Supplier<Stream<Map.Entry<String, Integer>>> sup = () -> map.entrySet().stream().filter((e) -> e.getValue() == longestSubword);
System.out.println(sup.get().count());
System.out.println("Pierwsze ze słów: ");
System.out.println(sup.get().findFirst().get().getKey());
}
}
1:
1. ćw.=1339
2. ćw.=274
3. ćw.=166
4. ćw.=218
poza=3
2:
158
3:
231
4:
6
3
11
7
4
5
12
36
18
53
87
21
73
39
31
12
41
10
64
22
57
31
41
50
65
73
45
18
37
Najdłuższy: 87
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Circles {
public static final class Circle {
public final double x, y, radius;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public boolean isPerpendicular(Circle circle) {
return this.radius == circle.radius && -this.x == circle.y && this.y == circle.x;
}
public boolean isTangent(Circle circle) {
double distSquared = (circle.x - this.x) * (circle.x - this.x) + (circle.y - this.y) * (circle.y - this.y);
return distSquared <= (this.radius + circle.radius) * (this.radius + circle.radius) && distSquared >= (this.radius - circle.radius) * (this.radius - circle.radius);
}
}
public static void main(String... args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("/mnt/development/projects/java/private/matura/97/src/okregi.txt"));
ArrayList<Circle> circles = new ArrayList<>();
while (scanner.hasNextLine()) {
circles.add(new Circle(scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble()));
}
//1
System.out.println("1:");
circles.stream().collect(Collectors.groupingBy((c) -> {
if (c.radius > Math.abs(c.x) || c.radius > Math.abs(c.y)) {
return "poza";
}
if (c.x >= 0) {
return c.y >= 0 ? "1. ćw." : "4. ćw.";
} else {
return c.y >= 0 ? "2. ćw." : "3. ćw.";
}
}, Collectors.counting())).entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(System.out::println);
//2
long x = circles.stream().distinct().collect(Collectors.groupingBy((c) -> Math.abs(c.x) + ":" + c.y + ":" + c.radius, Collectors.counting())).entrySet().stream().filter(k -> k.getValue() == 2).count();
long y = circles.stream().distinct().collect(Collectors.groupingBy((c) -> Math.abs(c.y) + ":" + c.x + ":" + c.radius, Collectors.counting())).entrySet().stream().filter(k -> k.getValue() == 2).count();
System.out.println("\n2:\n" + (x + y));
//3
int perps = 0;
for (int i = 0; i < circles.size(); i++) {
Circle circ = circles.get(i);
for (int j = 0; j < circles.size(); j++) {
if (circles.get(j).isPerpendicular(circ)) {
perps++;
break;
}
}
}
System.out.println("\n3:\n" + perps);
System.out.println("\n4:");
int length = 1, max = 1;
for (int i = 1; i < 1000; i++) {
if (circles.get(i).isTangent(circles.get(i - 1))) {
length++;
} else {
if (length > max) {
max = length;
}
System.out.println(length);
length = 1;
}
}
System.out.println("Najdłuższy: " + max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment