diff --git a/task01/src/com/example/task01/Task01Main.java b/task01/src/com/example/task01/Task01Main.java index de4599c1..d64a391c 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -1,29 +1,39 @@ package com.example.task01; -import java.io.IOException; +import java.util.Objects; import java.util.function.Function; import java.util.function.Predicate; public class Task01Main { - public static void main(String[] args) throws IOException { + public static void main(String[] args) { - // TODO С корректно реализованным методом ternaryOperator должен компилироваться и успешно работать следующий код: - /* Predicate condition = Objects::isNull; Function ifTrue = obj -> 0; Function ifFalse = CharSequence::length; Function safeStringLength = ternaryOperator(condition, ifTrue, ifFalse); - */ + + System.out.println(safeStringLength.apply(null)); // 0 + System.out.println(safeStringLength.apply("Hello")); // 5 } public static Function ternaryOperator( Predicate condition, Function ifTrue, - Function ifFalse) { - - return null; // your implementation here - + Function ifFalse) + { + + Objects.requireNonNull(condition, "Condition null"); + Objects.requireNonNull(ifTrue, "IfTrue null"); + Objects.requireNonNull(ifFalse, "IfFalse null"); + + return t -> { + if (condition.test(t)) { + return ifTrue.apply(t); + } else { + return ifFalse.apply(t); + } + }; } } diff --git a/task02/src/com/example/task02/Task02Main.java b/task02/src/com/example/task02/Task02Main.java index 309260d8..8c938498 100644 --- a/task02/src/com/example/task02/Task02Main.java +++ b/task02/src/com/example/task02/Task02Main.java @@ -2,22 +2,37 @@ import java.util.stream.IntStream; +import java.util.stream.IntStream; +import java.util.stream.Stream; + public class Task02Main { public static void main(String[] args) { - /* - cycleGrayCode(2) - .limit(10) - .forEach(System.out::println); - */ + cycleGrayCode(2).limit(10).forEach(System.out::println); + cycleGrayCode(3).limit(10).forEach(System.out::println); + cycleGrayCode(4).limit(10).forEach(System.out::println); } public static IntStream cycleGrayCode(int n) { + if (n < 1 || n > 16) { + throw new IllegalArgumentException("Error: n < 1 || n > 16"); + } - return null; // your implementation here + int[] grayCodes = generateGrayCodes(n); + return IntStream.iterate(0, i -> (i + 1) % grayCodes.length).map(i -> grayCodes[i]); } -} + private static int[] generateGrayCodes(int n) { + int size = 1 << n; + int[] grayCodes = new int[size]; + + for (int i = 0; i < size; i++) { + grayCodes[i] = i ^ (i >> 1); + } + + return grayCodes; + } +} \ No newline at end of file diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index 254d5cb0..4e3a3f5d 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -4,6 +4,9 @@ import java.util.function.BiConsumer; import java.util.stream.Stream; +import java.util.*; +import java.util.stream.Collectors; + public class Task03Main { public static void main(String[] args) { @@ -11,10 +14,14 @@ public static void main(String[] args) { findMinMax( Stream.of(2, 9, 5, 4, 8, 1, 3), Integer::compareTo, - (min, max) -> - System.out.println("min: " + min + " / max: " + max) + (min, max) -> System.out.println("min: " + min + " / max: " + max) ); + findMinMax( + Stream.empty(), + Integer::compareTo, + (min, max) -> System.out.println("min: " + min + " / max: " + max) + ); } public static void findMinMax( @@ -22,6 +29,36 @@ public static void findMinMax( Comparator order, BiConsumer minMaxConsumer) { - // your implementation here + Objects.requireNonNull(stream, "Stream null"); + Objects.requireNonNull(order, "Comparator null"); + Objects.requireNonNull(minMaxConsumer, "BiConsumer null"); + + Optional min = Optional.empty(); + Optional max = Optional.empty(); + + Iterator iterator = stream.iterator(); + + if (!iterator.hasNext()) { + minMaxConsumer.accept(null, null); + return; + } + + T first = iterator.next(); + min = Optional.of(first); + max = Optional.of(first); + + while (iterator.hasNext()) { + T current = iterator.next(); + + if (current == null || (min.isPresent() && order.compare(current, min.get()) < 0)) { + min = Optional.of(current); + } + + if (current == null || (max.isPresent() && order.compare(current, max.get()) > 0)) { + max = Optional.of(current); + } + } + + minMaxConsumer.accept(min.orElse(null), max.orElse(null)); } } diff --git a/task04/src/com/example/task04/Task04Main.java b/task04/src/com/example/task04/Task04Main.java index 1e38e6eb..29d0614a 100644 --- a/task04/src/com/example/task04/Task04Main.java +++ b/task04/src/com/example/task04/Task04Main.java @@ -1,11 +1,42 @@ package com.example.task04; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.ToLongFunction; +import java.util.stream.Collectors; +import java.util.stream.Stream; + public class Task04Main { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { - // your implementation here + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); + String input = reader.readLine().toLowerCase(); - } + String[] words = input.split("[^\\p{L}\\p{N}]+"); + + Map wordFrequency = Arrays.stream(words) + .collect(Collectors.groupingBy(word -> word, Collectors.counting())); -} + Map sortedByFrequencyAndAlphabet = wordFrequency + .entrySet() + .stream() + .sorted(Map.Entry.comparingByValue() + .reversed() + .thenComparing(Map.Entry.comparingByKey())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, + (oldValue, newValue) -> oldValue, LinkedHashMap::new)); + + sortedByFrequencyAndAlphabet.entrySet().stream() + .limit(10) + .map(Map.Entry::getKey) + .forEach(System.out::println); + + } +} \ No newline at end of file diff --git a/task05/src/com/example/task05/MailMessage.java b/task05/src/com/example/task05/MailMessage.java new file mode 100644 index 00000000..4cd37c47 --- /dev/null +++ b/task05/src/com/example/task05/MailMessage.java @@ -0,0 +1,27 @@ +package com.example.task05; + +public class MailMessage implements Sendable { + private final String from; + private final String to; + private final String content; + + public MailMessage(String from, String to, String content) { + this.from = from; + this.to = to; + this.content = content; + } + + public String getFrom() { + return from; + } + + @Override + public String getTo() { + return to; + } + + @Override + public String getContent() { + return content; + } +} \ No newline at end of file diff --git a/task05/src/com/example/task05/MailService.java b/task05/src/com/example/task05/MailService.java new file mode 100644 index 00000000..6642e1e1 --- /dev/null +++ b/task05/src/com/example/task05/MailService.java @@ -0,0 +1,18 @@ +package com.example.task05; + +import java.util.*; +import java.util.function.Consumer; +public class MailService implements Consumer> { + private final Map> mailBox = new HashMap<>(); + + public Map> getMailBox() { + return mailBox; + } + + @Override + public void accept(Sendable sendable) { + List list = mailBox.getOrDefault(sendable.getTo(), new ArrayList<>()); + list.add(sendable.getContent()); + mailBox.put(sendable.getTo(), list); + } +} \ No newline at end of file diff --git a/task05/src/com/example/task05/Salary.java b/task05/src/com/example/task05/Salary.java new file mode 100644 index 00000000..600d7955 --- /dev/null +++ b/task05/src/com/example/task05/Salary.java @@ -0,0 +1,27 @@ +package com.example.task05; + +public class Salary implements Sendable { + private final String from; + private final String to; + private final int amount; + + public Salary(String from, String to, int amount) { + this.from = from; + this.to = to; + this.amount = amount; + } + + public String getFrom() { + return from; + } + + @Override + public String getTo() { + return to; + } + + @Override + public Integer getContent() { + return amount; + } +} \ No newline at end of file diff --git a/task05/src/com/example/task05/Sendable.java b/task05/src/com/example/task05/Sendable.java new file mode 100644 index 00000000..0bf354f7 --- /dev/null +++ b/task05/src/com/example/task05/Sendable.java @@ -0,0 +1,6 @@ +package com.example.task05; + +public interface Sendable { + String getTo(); + T getContent(); +} \ No newline at end of file diff --git a/task05/src/com/example/task05/Task05Main.java b/task05/src/com/example/task05/Task05Main.java index ae31f6bc..9cbde9e1 100644 --- a/task05/src/com/example/task05/Task05Main.java +++ b/task05/src/com/example/task05/Task05Main.java @@ -4,16 +4,15 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.*; public class Task05Main { public static void main(String[] args) { - /* - // Random variables - String randomFrom = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно. - String randomTo = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно. + String randomFrom = "RandomSender"; // Некоторая случайная строка. Можете выбрать ее самостоятельно. + String randomTo = "RandomRecipient"; // Некоторая случайная строка. Можете выбрать ее самостоятельно. int randomSalary = 100; // Некоторое случайное целое положительное число. Можете выбрать его самостоятельно. // Создание списка из трех почтовых сообщений. @@ -86,10 +85,5 @@ public static void main(String[] args) { assert salaries.get(salary1.getTo()).equals(Arrays.asList(1)) : "wrong salaries mailbox content (1)"; assert salaries.get(salary2.getTo()).equals(Arrays.asList(Integer.MAX_VALUE)) : "wrong salaries mailbox content (2)"; assert salaries.get(randomTo).equals(Arrays.asList(randomSalary)) : "wrong salaries mailbox content (3)"; - - - */ - } - }