diff --git a/task01/src/com/example/task01/Task01Main.java b/task01/src/com/example/task01/Task01Main.java index de4599c1..cb49c357 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -1,6 +1,7 @@ package com.example.task01; import java.io.IOException; +import java.util.Objects; import java.util.function.Function; import java.util.function.Predicate; @@ -9,12 +10,12 @@ public static void main(String[] args) throws IOException { // TODO С корректно реализованным методом ternaryOperator должен компилироваться и успешно работать следующий код: - /* + Predicate condition = Objects::isNull; Function ifTrue = obj -> 0; Function ifFalse = CharSequence::length; Function safeStringLength = ternaryOperator(condition, ifTrue, ifFalse); - */ + } @@ -23,7 +24,17 @@ public static Function ternaryOperator( Function ifTrue, Function ifFalse) { - return null; // your implementation here - + if (condition == null || ifTrue == null || ifFalse == null) { + throw new NullPointerException("One of the parameters is null"); + } + + return input -> { + if (condition.test(input)){ + return ifTrue.apply(input); + } + else{ + return ifFalse.apply(input); + } + }; } -} +} \ No newline at end of file diff --git a/task02/src/com/example/task02/Task02Main.java b/task02/src/com/example/task02/Task02Main.java index 309260d8..e34d97e5 100644 --- a/task02/src/com/example/task02/Task02Main.java +++ b/task02/src/com/example/task02/Task02Main.java @@ -6,18 +6,24 @@ public class Task02Main { public static void main(String[] args) { - /* + cycleGrayCode(2) .limit(10) .forEach(System.out::println); - */ + } + // Число сдвигаем побитово вправо на 1. И исключающее или между исходным и получившимся числом public static IntStream cycleGrayCode(int n) { + if (n < 1 || n > 16){ + throw new IllegalArgumentException("Неправильное n"); + } - return null; // your implementation here + int limit = 1 << n; // 2 в степени n + return IntStream.iterate(0, i -> (i + 1) % limit) + .map(i -> i ^ (i >> 1)); // Преобразование в код грея } -} +} \ 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..c20fa523 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -1,7 +1,11 @@ package com.example.task03; +import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; +import java.util.List; import java.util.function.BiConsumer; +import java.util.stream.Collectors; import java.util.stream.Stream; public class Task03Main { @@ -20,8 +24,33 @@ public static void main(String[] args) { public static void findMinMax( Stream stream, Comparator order, - BiConsumer minMaxConsumer) { + BiConsumer minMaxConsumer) + { + if (stream == null || order == null || minMaxConsumer == null) { + throw new NullPointerException(); + } + + + Iterator iterator = stream.iterator(); + if (!iterator.hasNext()) { + minMaxConsumer.accept(null, null); + return; + } + + T first = iterator.next(); + T min = first; + T max = first; + + while (iterator.hasNext()) { + T element = iterator.next(); + if (order.compare(element, min) < 0) { + min = element; + } + if (order.compare(element, max) > 0) { + max = element; + } + } - // your implementation here + minMaxConsumer.accept(min, max); } -} +} \ No newline at end of file diff --git a/task04/src/com/example/task04/Task04Main.java b/task04/src/com/example/task04/Task04Main.java index 1e38e6eb..51e3d600 100644 --- a/task04/src/com/example/task04/Task04Main.java +++ b/task04/src/com/example/task04/Task04Main.java @@ -1,11 +1,32 @@ package com.example.task04; -public class Task04Main { - - public static void main(String[] args) { +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; - // your implementation here +public class Task04Main { + public static void main(String[] args) throws IOException { + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(System.in, StandardCharsets.UTF_8) + )) { + reader.lines() + .map(String::toLowerCase) + .flatMap(line -> Arrays.stream(line.split("[^a-zа-яё0-9]+"))) + .filter(s -> !s.isEmpty()) + .collect(Collectors.groupingBy(word -> word, Collectors.counting())) + .entrySet() + .stream() + .sorted(Map.Entry.comparingByValue() + .reversed() + .thenComparing(Map.Entry.comparingByKey())) + .limit(10) + .map(Map.Entry::getKey) + .forEach(word -> System.out.print(word + "\n")); + } } - -} +} \ 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..6b455be1 100644 --- a/task05/src/com/example/task05/Task05Main.java +++ b/task05/src/com/example/task05/Task05Main.java @@ -1,15 +1,12 @@ package com.example.task05; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Consumer; public class Task05Main { public static void main(String[] args) { - /* // Random variables String randomFrom = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно. @@ -88,8 +85,96 @@ public static void main(String[] args) { assert salaries.get(randomTo).equals(Arrays.asList(randomSalary)) : "wrong salaries mailbox content (3)"; - */ + + } + +} + +interface Sendable{ + String getFrom(); + String getTo(); + T getContent(); +} + +class MailMessage implements Sendable{ + private String from; + private String to; + + private String content; + + public MailMessage(String from, String to, String content){ + this.from = from; + this.to = to; + this.content = content; + } + + public String getFrom(){ + return from; + } + + public String getTo(){ + return to; + } + + public String getContent(){ + return content; } } + +class Salary implements Sendable{ + private Integer salary; + private String from; + private String to; + public Salary(String from, String to, Integer salary){ + this.salary = salary; + this.from = from; + this.to = to; + } + + public Integer getContent(){ + return salary; + } + + public String getFrom(){ + return from; + } + public String getTo(){ + return to; + } +} + +class MailService implements Consumer>{ + private Map> mailbox = new HashMap<>(); + + public void accept(Sendable sendable){ + if (mailbox.containsKey(sendable.getTo())){ + List temp = mailbox.get(sendable.getTo()); + temp.add(sendable.getContent()); + mailbox.replace(sendable.getTo(), temp); + } + else{ + List temp = new ArrayList<>(); + temp.add(sendable.getContent()); + mailbox.put(sendable.getTo(), temp); + } + } + + public Map> getMailBox(){ + return new AbstractMap>() { + @Override + public Set>> entrySet() { + return mailbox.entrySet(); + } + + @Override + public List get(Object key) { + List value = mailbox.get(key); + return value == null ? Collections.emptyList() : value; + } + }; + } + + +}