Skip to content
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
8 changes: 4 additions & 4 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,12 +10,12 @@ public static void main(String[] args) throws IOException {

// TODO С корректно реализованным методом ternaryOperator должен компилироваться и успешно работать следующий код:

/*

Predicate<Object> condition = Objects::isNull;
Function<Object, Integer> ifTrue = obj -> 0;
Function<CharSequence, Integer> ifFalse = CharSequence::length;
Function<String, Integer> safeStringLength = ternaryOperator(condition, ifTrue, ifFalse);
*/


}

Expand All @@ -23,7 +24,6 @@ public static <T, U> Function<T, U> ternaryOperator(
Function<? super T, ? extends U> ifTrue,
Function<? super T, ? extends U> ifFalse) {

return null; // your implementation here

return t -> condition.test(t) ? ifTrue.apply(t) : ifFalse.apply(t); // your implementation here
}
}
17 changes: 11 additions & 6 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ public class Task02Main {

public static void main(String[] args) {

/*

cycleGrayCode(2)
.limit(10)
.forEach(System.out::println);
*/


}

public static IntStream cycleGrayCode(int n) {

return null; // your implementation here

if (n > 16 || n < 1) {
throw new IllegalArgumentException();
}

IntStream stream = IntStream
.iterate(0,
i -> (i + 1) % (int) Math.pow(2, n))
.map(i -> i ^ (i >> 1));
return stream; // your implementation here
}

}
30 changes: 29 additions & 1 deletion task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.example.task03;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class Task03Main {
Expand All @@ -14,14 +18,38 @@ public static void main(String[] args) {
(min, max) ->
System.out.println("min: " + min + " / max: " + max)
);

}

public static <T> void findMinMax(
Stream<? extends T> stream,
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {

Iterator<? extends T> iterator = stream.iterator();

T min = null;
T max = null;

if (iterator.hasNext()) {
min = iterator.next();
max = min;
}

while (iterator.hasNext()) {
T a = iterator.next();

int compMin = order.compare(a, min);
int compMax = order.compare(a, max);

if (compMin <= 0) {
min = a;
}
if (compMax >= 0) {
max = a;
}
}

minMaxConsumer.accept(min, max);
// your implementation here
}
}
29 changes: 28 additions & 1 deletion task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
package com.example.task04;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Task04Main {

public static void main(String[] args) {

// your implementation here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Stream<String> stream = reader.lines();

}
String result = stream
.flatMap(line -> Arrays.stream(
line.split("[^\\p{L}\\p{N}]+")))
.map(String::toLowerCase)
.filter(word -> !word.isEmpty())
.collect(Collectors.groupingBy(
word -> word,
Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.<String, Long>comparingByValue()
.reversed()
.thenComparing(Map.Entry.comparingByKey()))
.limit(10)
.map(Map.Entry::getKey)
.collect(Collectors.joining("\n"));

System.out.println(result);
}
}
5 changes: 2 additions & 3 deletions task05/resources/TestsImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.task05;

import com.example.task05.ITests;
import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -47,7 +46,7 @@ public void testExample() {
);

// Создание почтового сервиса.
MailService<String> mailService = new MailService<>();
MailService mailService = new MailService();

// Обработка списка писем почтовым сервисом
messages.stream().forEachOrdered(mailService);
Expand Down Expand Up @@ -78,7 +77,7 @@ public void testExample() {
Salary salary3 = new Salary(randomFrom, randomTo, randomSalary);

// Создание почтового сервиса, обрабатывающего зарплаты.
MailService<Integer> salaryService = new MailService<>();
MailService salaryService = new MailService();

// Обработка списка зарплат почтовым сервисом
Arrays.asList(salary1, salary2, salary3).forEach(salaryService);
Expand Down
12 changes: 12 additions & 0 deletions task05/src/com/example/task05/MailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.task05;

public class MailMessage extends TypeMessage<String> {

public MailMessage(String from, String to, String content) {
super(from, to, content);
}

public String getContent() {
return super.getMessage();
}
}
28 changes: 28 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task05;

import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Consumer;

public class MailService<T> implements Consumer<TypeMessage<T>> {

Map<String, List<T>> mailBox = new HashMap<String, List<T>>() {
@Override
public List<T> get(Object key) {
List<T> list = super.get(key);
if (list == null) return new ArrayList<>();
return list;
}
};

@Override
public void accept(TypeMessage<T> message) {
String to = message.getTo();
T m = message.getMessage();
mailBox.computeIfAbsent(to, key -> new ArrayList<>()).add(m);
}

public Map<String, List<T>> getMailBox() {
return mailBox;
}
}
8 changes: 8 additions & 0 deletions task05/src/com/example/task05/Salary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.task05;

public class Salary extends TypeMessage<Integer> {

public Salary (String from, String to, int salary) {
super(from, to, salary);
}
}
13 changes: 3 additions & 10 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ public class Task05Main {

public static void main(String[] args) {

/*

// Random variables
String randomFrom = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomTo = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomFrom = "Robert Howard"; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomTo = "My Friend"; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
int randomSalary = 100; // Некоторое случайное целое положительное число. Можете выбрать его самостоятельно.

// Создание списка из трех почтовых сообщений.
Expand Down Expand Up @@ -44,7 +42,7 @@ public static void main(String[] args) {
);

// Создание почтового сервиса.
MailService<String> mailService = new MailService<>();
MailService mailService = new MailService();

// Обработка списка писем почтовым сервисом
messages.stream().forEachOrdered(mailService);
Expand Down Expand Up @@ -86,10 +84,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)";


*/

}

}
26 changes: 26 additions & 0 deletions task05/src/com/example/task05/TypeMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.task05;

public class TypeMessage<T> {

final private String from;
final private String to;
final private T message;

public TypeMessage(String from, String to, T message) {
this.from = from;
this.to = to;
this.message = message;
}

public String getFrom() {
return from;
}

public String getTo() {
return to;
}

public T getMessage() {
return message;
}
}