From effb87f8b14b34ceb2c3095349826d4b028ecd02 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 Oct 2018 21:56:13 +0300 Subject: [PATCH 1/3] P3221 Denis Baycerov --- .../cet/javabasics/WarAndPeaceExercise.java | 63 ++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 1ff35c2..ecc6801 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -1,21 +1,70 @@ package ru.ifmo.cet.javabasics; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.*; public class WarAndPeaceExercise { - public static String warAndPeace() { + public static String warAndPeace() throws IOException { final Path tome12Path = Paths.get("src", "main", "resources", "WAP12.txt"); final Path tome34Path = Paths.get("src", "main", "resources", "WAP34.txt"); - // TODO map lowercased words to its amount in text and concatenate its entries. - // TODO If word "котик" occurred in text 23 times then its entry would be "котик - 23\n". - // TODO Entries in final String should be also sorted by amount and then in alphabetical order if needed. - // TODO Also omit any word with lengths less than 4 and frequency less than 10 + HashMap dictionary=new HashMap(); + final Charset charset = Charset.forName("windows-1251"); - throw new UnsupportedOperationException(); - } + List strings = Files.readAllLines(tome12Path, charset); + strings.addAll(Files.readAllLines(tome34Path, charset)); + for (String string: strings) { + string=string.toLowerCase(); + string=string.replaceAll("[^A-Za-z\u0410-\u042f\u0430-\u044f]", " "); + String[] words=string.split(" "); + + + for (String word:words) { + if(word.length()<4) continue; + Integer count=dictionary.get(word); + if (dictionary.get(word) == null) { + dictionary.put(word,1); + }else{ + dictionary.put(word,++count); + } + } + } + int g=4; + dictionary.entrySet().removeIf(entry -> entry.getValue() < 10); + int c= "разговором".compareTo("пусть"); + + //сортировка и отбор в алфавитном порядке + ArrayList list_result=new ArrayList(); + while (!dictionary.isEmpty()){ + HashMap.Entry Max_entry=dictionary.entrySet().iterator().next(); + for (Map.Entry entry : dictionary.entrySet()) { + if (Max_entry.getValue() < entry.getValue()) { + Max_entry = entry; + } else if (Max_entry.getValue().equals(entry.getValue())) { + if (Max_entry.getKey().compareTo(entry.getKey()) > 0) { + Max_entry = entry; + } + } + } + String string=Max_entry.getKey()+ " - "+Max_entry.getValue(); + list_result.add(string); + dictionary.remove(Max_entry.getKey()); + } + + String result=""; + for (String s : list_result) + { + result += s + "\n"; + } + result=result.substring(0,result.length()-1); + return result; + } } \ No newline at end of file From 303a21eaf44fb6366b15be9c2f85cc5cccd8487f Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 Oct 2018 21:57:04 +0300 Subject: [PATCH 2/3] P3221 Denis Baycerov 2.0 --- .idea/vcs.xml | 6 ++++++ .../java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java | 4 ---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index ecc6801..884ec7d 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -36,11 +36,7 @@ public static String warAndPeace() throws IOException { } } } - int g=4; dictionary.entrySet().removeIf(entry -> entry.getValue() < 10); - int c= "разговором".compareTo("пусть"); - - //сортировка и отбор в алфавитном порядке ArrayList list_result=new ArrayList(); while (!dictionary.isEmpty()){ From cf69d87151e5ed291fe73e5a4bece95cc3d42c0e Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 12 Oct 2018 22:34:54 +0300 Subject: [PATCH 3/3] P3221 Denis Baycerov 3.0 --- .../ru/ifmo/cet/javabasics/WarAndPeaceExercise.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 884ec7d..9a8c4a7 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -1,6 +1,5 @@ package ru.ifmo.cet.javabasics; -import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; @@ -15,7 +14,7 @@ public static String warAndPeace() throws IOException { final Path tome12Path = Paths.get("src", "main", "resources", "WAP12.txt"); final Path tome34Path = Paths.get("src", "main", "resources", "WAP34.txt"); - HashMap dictionary=new HashMap(); + HashMap dictionary= new HashMap<>(); final Charset charset = Charset.forName("windows-1251"); List strings = Files.readAllLines(tome12Path, charset); @@ -36,9 +35,14 @@ public static String warAndPeace() throws IOException { } } } - dictionary.entrySet().removeIf(entry -> entry.getValue() < 10); + for(Iterator> it=dictionary.entrySet().iterator();it.hasNext();) { + HashMap.Entry entry = it.next(); + if (entry.getValue() < 10) { + it.remove(); + } + } //сортировка и отбор в алфавитном порядке - ArrayList list_result=new ArrayList(); + ArrayList list_result= new ArrayList<>(); while (!dictionary.isEmpty()){ HashMap.Entry Max_entry=dictionary.entrySet().iterator().next(); for (Map.Entry entry : dictionary.entrySet()) {