Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed test commit #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import java.util.*;

import static java.util.Collections.*;

/**
* Совет:
* Начните с реализации метода {@link SimpleTextStatisticsAnalyzer#getWords(String)}.
Expand All @@ -23,7 +21,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
int count = 0;
List<String> list = new ArrayList<>(getWords(text));
for (String s : list) {
count += s.length();
}
return count;
}

/**
Expand All @@ -34,7 +37,8 @@ public int countSumLengthOfWords(String text) {
*/
@Override
public int countNumberOfWords(String text) {
return 0;
List<String> list = new ArrayList<>(getWords(text));
return list.size();
}

/**
Expand All @@ -44,7 +48,8 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
Set<String> set = new HashSet<>(getWords(text));
return set.size();
}

/**
Expand All @@ -57,7 +62,14 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
List<String> list = new ArrayList<>();
String regex = "\\W";
for (String s : text.split(regex)) {
if (!s.equals("")) {
list.add(s);
}
}
return list;
}

/**
Expand All @@ -70,7 +82,7 @@ public List<String> getWords(String text) {
*/
@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
return new HashSet<>(getWords(text));
}

/**
Expand All @@ -82,7 +94,13 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
ArrayList<String> list = new ArrayList<>(getWords(text));
Map<String, Integer> map = new HashMap<>();
for (String s : list) {
int count = Collections.frequency(list, s);
map.put(s, count);
}
return map;
}

/**
Expand All @@ -95,6 +113,13 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> list = new ArrayList<>(getWords(text));
if (direction.equals(Direction.ASC)) {
list.sort(Comparator.comparing(String::length));
} else if (direction.equals(Direction.DESC)) {
list.sort(Comparator.comparing(String::length));
Collections.reverse(list);
}
return list;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.epam.izh.rd.online.helper.Direction;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.*;

/**
* Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична
Expand All @@ -16,36 +15,55 @@
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
@Override
public int countSumLengthOfWords(String text) {
return 0;
List<String> list = new ArrayList<>(getWords(text));
Stream<String> stream = list.stream();
int count = stream.mapToInt(String::length).sum();
return count;
}

@Override
public int countNumberOfWords(String text) {
return 0;
List<String> list = new ArrayList<>(getWords(text));
Stream<String> stream = list.stream();
return (int) stream.count();
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
Set<String> set = new HashSet<>(getWords(text));
Stream<String> stream = set.stream();
return (int) stream.count();
}

@Override
public List<String> getWords(String text) {
return emptyList();
String regex = "\\W";
List<String> list = Arrays.stream(text.split(regex)).collect(Collectors.toList());
list.removeIf(String::isEmpty);
return list;
}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
ArrayList<String> list = new ArrayList<>(getWords(text));
return list.stream().collect(Collectors.toSet());
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
ArrayList<String> list = new ArrayList<>(getWords(text));
return list.stream().collect(Collectors.toMap(Function.identity(), x -> 1, Integer::sum));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> list = new ArrayList<>(getWords(text));
if (direction.equals(Direction.ASC)) {
list = list.stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList());
} else if (direction.equals(Direction.DESC)) {
list = list.stream().sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList());
}
return list;

}
}