feat(09-popular-patterns): Добавлено задание

This commit is contained in:
Mark Zheleznyakov
2024-12-11 13:24:26 +03:00
parent b145a859c0
commit f9c8727c97
14 changed files with 549 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.10.2/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used by the application.
implementation(libs.guava)
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(23)
}
}
application {
// Define the main class for the application.
mainClass = "ru.mrqiz.patterns.App"
}
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
tasks.named<JavaExec>("run") {
standardInput = System.`in`
}

View File

@ -0,0 +1,36 @@
package ru.mrqiz.patterns;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PatternAnalyzer analyzer = new PatternAnalyzer();
PatternSorter sorter = new PatternSorter();
while (true) {
String line = in.nextLine();
if (line.isEmpty()) {
break;
}
analyzer.analyzeLine(line);
}
Map<Integer, List<Map.Entry<String, Integer>>> topPatterns = sorter.sortPatterns(analyzer.getPatterns());
for (int length = 1; length <= 3; length++) {
System.out.println("len " + length + ":");
List<Map.Entry<String, Integer>> entries = topPatterns.get(length);
if (entries != null) {
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
in.close();
}
}

View File

@ -0,0 +1,24 @@
package ru.mrqiz.patterns;
import java.util.HashMap;
import java.util.Map;
public class PatternAnalyzer {
private final HashMap<String, Integer> patterns = new HashMap<>();
public void analyzeLine(String line) {
for (int i = 0; i < line.length(); i++) {
for (int length = 1; length <= 3; length++) {
if (i + length <= line.length()) {
String pattern = line.substring(i, i + length);
patterns.put(pattern, patterns.getOrDefault(pattern, 0) + 1);
}
}
}
}
public Map<String, Integer> getPatterns() {
return patterns;
}
}

View File

@ -0,0 +1,35 @@
package ru.mrqiz.patterns;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PatternSorter {
public Map<Integer, List<Map.Entry<String, Integer>>> sortPatterns(Map<String, Integer> patterns) {
Map<Integer, List<Map.Entry<String, Integer>>> patternsByLength = new HashMap<>();
for (Map.Entry<String, Integer> entry : patterns.entrySet()) {
int length = entry.getKey().length();
patternsByLength.putIfAbsent(length, new ArrayList<>());
patternsByLength.get(length).add(entry);
}
Map<Integer, List<Map.Entry<String, Integer>>> topPatterns = new HashMap<>();
for (int length : patternsByLength.keySet()) {
List<Map.Entry<String, Integer>> sortedEntries = patternsByLength.get(length);
sortedEntries.sort(Comparator.comparingInt(Map.Entry<String, Integer>::getValue)
.reversed()
.thenComparing(Map.Entry.comparingByKey()));
List<Map.Entry<String, Integer>> topEntries = new ArrayList<>();
for (int i = 0; i < Math.min(3, sortedEntries.size()); i++) {
topEntries.add(sortedEntries.get(i));
}
topPatterns.put(length, topEntries);
}
return topPatterns;
}
}