import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΡΠΏΠΈΡΠΎΠΊ Π»ΡΠ΄Π΅ΠΉ Ρ ΡΠ°ΠΌΠΈΠ»ΠΈΠ΅ΠΉ ΠΈΠ»ΠΈ ΠΈΠΌΠ΅Π½Π΅ΠΌ King
*/
public static List<String> methodOne(List<String> names) {
return names.stream()
.filter(s -> s.contains("King"))
.collect(Collectors.toList());
}
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΡ
ΡΠ°ΠΌΠΈΠ»ΠΈΠΉ (2 ΠΏΠΎΠ·ΠΈΡΠΈΡ Π² ΡΡΡΠΎΠΊΠ΅)
*/
public static List<String> methodTwo(List<String> names) {
return names.stream()
.map(s -> s.substring(s.indexOf(" ") + 1))
.distinct()
.collect(Collectors.toList());
}
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΡ
ΠΈΠΌΠ΅Π½, Π½Π°ΡΠΈΠ½Π°ΡΡΠΈΡ
ΡΡ Ρ L
*/
public static List<String> methodThree(List<String> names) {
return names.stream()
.filter(s -> s.startsWith("L"))
.map(s -> s.substring(0, s.indexOf(" ")))
.distinct()
.collect(Collectors.toList());
}
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΡ
ΡΠ°ΠΌΠΈΠ»ΠΈΠΉ, Π½Π°ΡΠΈΠ½Π°ΡΡΠΈΡ
ΡΡ Ρ W, ΠΎΡΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΏΠΎ Π°Π»ΡΠ°Π²ΠΈΡΡ
*/
public static List<String> methodFour(List<String> names) {
return names.stream()
.map(s -> s.substring(s.indexOf(" ") + 1))
.filter(s ->s.startsWith("W"))
.distinct()
.sorted()
.collect(Collectors.toList());
}
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΈΠΌΠ΅Π½ Π΄Π»ΠΈΠ½Π΅Π΅ ΡΠ΅ΠΌ 6 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ²
*/
public static Integer methodFive(List<String> names) {
return (int) names.stream()
.map(s -> s.substring(0, s.indexOf(" ")))
.filter(s -> s.length() > 6)
.distinct()
.count();
}
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΡΡΠΌΠΌΠ°ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΡ
ΠΈΠΌΡΠ½ ΠΈ ΡΠ°ΠΌΠΈΠ»ΠΈΠΉ, Π΄Π»ΠΈΠ½Π½Π΅Π΅ 5 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ²
*/
public static Integer methodSix(List<String> names) {
return (int) names.stream()
.flatMap(s -> Arrays.stream(s.split("\\s+")))
.filter(s -> s.length() > 5)
.distinct()
.count();
}
/**
* ΠΠ΅ΡΠΎΠ΄ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π΅ΡΠ½ΡΡΡ ΠΈΠΌΠ΅Π½Π° Π»ΡΠ΄Π΅ΠΉ, ΡΡΡ ΡΠ°ΠΌΠΈΠ»ΠΈΡ Π½Π°ΡΠΈΠ½Π°Π΅ΡΡΡ Ρ K ΠΈΠ»ΠΈ S, Π² ΡΠΎΡΠΌΠ°ΡΠ΅ ΠΈΠΌΡ + ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠΊΠ²Π° ΡΠ°ΠΌΠΈΠ»ΠΈΠΈ,
* Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ "David Z.". Π‘ΠΏΠΈΡΠΎΠΊ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΡΡ ΠΎΡΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½ ΠΏΠΎ Π°Π»ΡΠ°Π²ΠΈΡΡ ΠΈ ΡΠΎΡΡΠΎΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ· ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΡ
Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ
*/
public static List<String> methodSeven(List<String> names) {
return names.stream()
.filter(s -> s.matches("\\w+ [KS]\\w+"))
.map(s -> s.substring(0, s.indexOf(" ") + 2) + ".")
.sorted()
.distinct()
.collect(Collectors.toList());
}
public static void main(String[] args) {
printTestCase(KINGS, methodOne(FULL_NAMES));
printTestCase(UNIQ_SURNAMES, methodTwo(FULL_NAMES));
printTestCase(NAMES_STARTS_WITH_L, methodThree(FULL_NAMES));
printTestCase(SURNAMES_STARTS_WITH_W_SORTED, methodFour(FULL_NAMES));
printTestCase(NAMES_LONGER_THAN_SIX, methodFive(FULL_NAMES));
printTestCase(NAMES_AND_SURNAMES_LONGER_THAN_FIVE, methodSix(FULL_NAMES));
printTestCase(NAMES_WITH_LETTER_SORTED, methodSeven(FULL_NAMES));
}
private static final List<String> FULL_NAMES = List.of(
"Joseph Smith", "Thomas Doyle", "Ronald Pratt", "Thomas Spencer", "King Lee",
"Gregory Smith", "Leroy Zimmerman", "Lee Smith", "Michael Harrington", "Lee Daniels",
"Eugene Williams", "Lee Terry", "Arnold Fowler", "Billy Harrison", "Lee Bennett", "Lee Evans",
"Ronald Diaz", "Leonard King", "Timothy Smith", "George Lee", "King Mann", "Dean Wright",
"Lee Richards", "Arthur Smith", "Gregory Moore", "Larry Hall", "Patrick Moore", "Ben Smith",
"Jose Smith", "Michael Parker", "Jeff Smith", "Larry Thompson", "Joel Smith", "Ivan Spencer",
"Robert Anderson", "Lee Phillips", "Stanley Warren", "William Sanchez", "Jamie McDonald",
"David Griffith", "Terry Hicks", "Larry Johnson", "Mike Fowler", "Ivan Shaw", "Lee Stevenson",
"Lee Powell", "Larry King", "Lee Mitchell", "Robert Diaz", "John Fowler", "Todd Cox",
"David James", "Willie Bryan", "Thomas Howard", "Ivan Stanley", "Paul Harper", "Robert King",
"David Stanley", "Dale Fletcher", "Larry Mullins", "Arthur King", "Robert King", "John King",
"Larry Coleman", "Anthony King", "Ivan Johnson", "Ivan Mann", "Michael Diaz", "Leroy Smith",
"King Wagner", "David Stanley", "Ivan Willis", "Ivan Stewart", "Frank King", "Roland Jones",
"Leroy Warren", "Rodney Bailey", "David King", "Randy King", "James Soto", "James Parker",
"Bobby Stanley", "Leroy Cooper", "Michael Woods", "David Mann", "Lee Padilla", "Samuel Shelton",
"Jesse Simmons", "Ivan Stanley", "Lee Gibson", "Bryan King", "David Brooks", "David Fowler",
"Robert Fowler", "Ivan King", "Donald Thomas", "Leroy Ramsey", "Russell Diaz", "David Torres"
);
private static final List<String> KINGS = List.of(
"King Lee", "Leonard King", "King Mann", "Larry King", "Robert King", "Arthur King",
"Robert King", "John King", "Anthony King", "King Wagner", "Frank King", "David King", "Randy King",
"Bryan King", "Ivan King"
);
private static final List<String> UNIQ_SURNAMES = List.of(
"Smith", "Doyle", "Pratt", "Spencer",
"Lee", "Zimmerman", "Harrington", "Daniels", "Williams", "Terry", "Fowler", "Harrison",
"Bennett", "Evans", "Diaz", "King", "Mann", "Wright", "Richards", "Moore", "Hall", "Parker",
"Thompson", "Anderson", "Phillips", "Warren", "Sanchez", "McDonald", "Griffith", "Hicks",
"Johnson", "Shaw", "Stevenson", "Powell", "Mitchell", "Cox", "James", "Bryan", "Howard",
"Stanley", "Harper", "Fletcher", "Mullins", "Coleman", "Wagner", "Willis", "Stewart",
"Jones", "Bailey", "Soto", "Cooper", "Woods", "Padilla", "Shelton", "Simmons", "Gibson",
"Brooks", "Thomas", "Ramsey", "Torres"
);
private static final List<String> NAMES_STARTS_WITH_L = List.of(
"Leroy", "Lee", "Leonard", "Larry"
);
private static final List<String> SURNAMES_STARTS_WITH_W_SORTED = List.of(
"Wagner", "Warren", "Williams", "Willis", "Woods", "Wright"
);
private static final List<String> NAMES_WITH_LETTER_SORTED = List.of(
"Anthony K.", "Arthur K.", "Arthur S.", "Ben S.", "Bobby S.", "Bryan K.", "David K.",
"David S.", "Frank K.", "Gregory S.", "Ivan K.", "Ivan S.", "James S.", "Jeff S.", "Jesse S.",
"Joel S.", "John K.", "Jose S.", "Joseph S.", "Larry K.", "Lee S.", "Leonard K.", "Leroy S.",
"Randy K.", "Robert K.", "Samuel S.", "Thomas S.", "Timothy S.", "William S."
);
private static final Integer NAMES_LONGER_THAN_SIX = 9;
private static final Integer NAMES_AND_SURNAMES_LONGER_THAN_FIVE = 61;
public static String constLen(String str, int len) {
StringBuilder sb = new StringBuilder(str);
while (len-- - str.length() > 0) sb.append(" ");
return sb.toString();
}
public static void printTestCase(List<String> exp, List<String> act) {
System.out.println();
Function<String, String> green = str -> "\u001B[32m" + str + "\u001B[0m";
Function<String, String> yellow = str -> "\u001B[33m" + str + "\u001B[0m";
System.out.println("ΠΠΆΠΈΠ΄Π°Π½ΠΈΠ΅: " + yellow.apply(constLen(exp.toString(), 15)));
System.out.println("Π Π΅Π°Π»ΡΠ½ΠΎΡΡΡ: " + green.apply(constLen(act.toString(), 15)));
System.out.print("RESULT ");
if (Objects.equals(exp, act)) System.out.print("β
"); else System.out.print("β");
System.out.println();
}
public static void printTestCase(Integer exp, Integer act) {
System.out.println();
Function<String, String> green = str -> "\u001B[32m" + str + "\u001B[0m";
Function<String, String> yellow = str -> "\u001B[33m" + str + "\u001B[0m";
System.out.println("ΠΠΆΠΈΠ΄Π°Π½ΠΈΠ΅: " + yellow.apply(constLen(exp.toString(), 15)));
System.out.println("Π Π΅Π°Π»ΡΠ½ΠΎΡΡΡ: " + green.apply(constLen(act.toString(), 15)));
System.out.print("RESULT ");
if (Objects.equals(exp, act)) System.out.print("β
"); else System.out.print("β");
System.out.println();
}
}