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
17 changes: 14 additions & 3 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,17 @@ 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 input -> {
if (condition.test(input)) {
return ifTrue.apply(input);
} else {
return ifFalse.apply(input);
}
};

}
}
9 changes: 8 additions & 1 deletion task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ 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("Invalid bit rate.");
}

int size = (int) Math.pow(2, n);

return IntStream.iterate(0, code -> (code + 1) % size)
.map(code -> code ^ (code >> 1));

}

Expand Down
25 changes: 24 additions & 1 deletion task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Comparator;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import java.util.Iterator;

public class Task03Main {

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

// your implementation here
Iterator<? extends T> iterator = stream.iterator();

T minimum = null;
T maximum = null;

if (iterator.hasNext()) {
minimum = iterator.next();
maximum = iterator.next();

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

if (order.compare(current, minimum) < 0) {
minimum = current;
}

if (order.compare(current, maximum) > 0) {
maximum = current;
}
}
}

minMaxConsumer.accept(minimum, maximum);
}
}
21 changes: 20 additions & 1 deletion task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package com.example.task04;

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

public class Task04Main {

public static void main(String[] args) {

// your implementation here
new BufferedReader(new InputStreamReader(System.in))
.lines()
.map(String::toLowerCase)
.filter(line -> !line.isEmpty())
.flatMap(line -> Stream.of(line.split("[\\p{Punct}\\s]+")))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(10)
.forEach(word -> System.out.println(word.getKey()));

}

Expand Down
7 changes: 7 additions & 0 deletions task05/src/com/example/task05/Dispatchable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.task05;

public interface Dispatchable<T> {
String getTo();
String getFrom();
T getContent();
}
28 changes: 28 additions & 0 deletions task05/src/com/example/task05/MailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task05;

public class MailMessage implements Dispatchable<String> {
final private String from;
final private String to;
final private String content;

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

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

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

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

import java.util.*;
import java.util.function.Consumer;

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

private final Map<String, List<T>> mailBox = new HashMap<String, List<T>>() {
@Override
public List<T> get(Object key) {
return super.getOrDefault(key, Collections.emptyList());
}
};

@Override
public void accept(Dispatchable<T> mail) {
mailBox
.computeIfAbsent(mail.getTo(), key -> new ArrayList<>())
.add(mail.getContent());
}

public Map<String, List<T>> getMailBox() {
return mailBox;
}
}
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 Dispatchable<Integer> {
final private String from;
final private String to;
final private Integer amount;

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

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

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

@Override
public Integer getContent() {
return amount;
}
}
4 changes: 2 additions & 2 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Task05Main {

public static void main(String[] args) {

/*


// Random variables
String randomFrom = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
Expand Down Expand Up @@ -88,7 +88,7 @@ public static void main(String[] args) {
assert salaries.get(randomTo).equals(Arrays.asList(randomSalary)) : "wrong salaries mailbox content (3)";


*/


}

Expand Down