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
6 changes: 5 additions & 1 deletion task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ 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
if (condition == null || ifTrue == null || ifFalse == null) {
throw new NullPointerException();
}

return t -> condition.test(t) ? ifTrue.apply(t) : ifFalse.apply(t);

}
}
7 changes: 6 additions & 1 deletion task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ public static void main(String[] args) {

public static IntStream cycleGrayCode(int n) {

return null; // your implementation here
if (n < 1 || n > 16) throw new IllegalArgumentException();
return IntStream.iterate(0, i -> i + 1).map(i -> grayCodeFromBinary(i % (int) Math.pow(2, n)));

}

public static int grayCodeFromBinary(int g) {
return g ^ (g >> 1);
}

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

import java.util.Comparator;
import java.util.Iterator;
import java.util.function.BiConsumer;
import java.util.stream.Stream;

Expand All @@ -22,6 +23,28 @@ public static <T> void findMinMax(
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {

// your implementation here
if (stream == null || order == null || minMaxConsumer == null) {
throw new NullPointerException();
}

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

if (!iterator.hasNext()) {
minMaxConsumer.accept(null, null);
} else {
T min = iterator.next();
T max = min;

while (iterator.hasNext()) {
T current = iterator.next();
if (order.compare(current, min) < 0) {
min = current;
} else if (order.compare(current, max) > 0) {
max = current;
}
}

minMaxConsumer.accept(min, max);
}
}
}
23 changes: 20 additions & 3 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package com.example.task04;

import java.io.BufferedReader;
import java.io.InputStreamReader;
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));
reader.lines()
.flatMap(x -> Stream.of(x.split("\\P{L}"))
.filter(t -> !t.isEmpty())
.map(String::toLowerCase))
.collect(Collectors.groupingBy(t -> t, Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(10)
.map(Map.Entry::getKey)
.forEach(t -> System.out.print(t + '\n'));
}

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

public class MailMessage implements Message<String> {
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;
}
}
35 changes: 35 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.task05;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

public class MailService<T> implements Consumer<Message<T>> {
HashMap<String, List<T>> mailBox = new HashMap<String, List<T>>() {

@Override
public List<T> get(Object key) {
return super.get(key) == null ? new ArrayList<>() : super.get(key);
}
};

public MailService() {

}

public HashMap<String, List<T>> getMailBox() {
return mailBox;
}

@Override
public void accept(Message<T> message) {
if (!mailBox.containsKey(message.getTo())) {
mailBox.put(message.getTo(), new ArrayList<>());
mailBox.get(message.getTo()).add(message.getContent());
} else {
mailBox.get(message.getTo()).add(message.getContent());
}
}

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

public interface Message<T> {
String getFrom();

String getTo();

T getContent();
}
28 changes: 28 additions & 0 deletions task05/src/com/example/task05/Salary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task05;

public class Salary implements Message<Integer> {
private String from;
private String to;
private Integer content;

public Salary(String from, String to, Integer content) {
this.from = from;
this.to = to;
this.content = content;
}

@Override
public String getFrom() {
return from;
}

@Override
public String getTo() {
return to;
}

@Override
public Integer getContent() {
return content;
}
}