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: 12 additions & 5 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,21 +10,27 @@ 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);
*/


}

public static <T, U> Function<T, U> ternaryOperator(
Predicate<? super T> condition,
Function<? super T, ? extends U> ifTrue,
Function<? super T, ? extends U> ifFalse) {

return null; // your implementation here
Function<? super T, ? extends U> ifFalse)
{
if(condition == null || ifTrue == null || ifFalse == null)
throw new NullPointerException();

return arg -> {
if(!condition.test(arg)) return ifFalse.apply(arg);
return ifTrue.apply(arg);
};

}
}
15 changes: 10 additions & 5 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ 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
public static IntStream cycleGrayCode(int n)
{
if(!(n <= 16 && n >= 1))
throw new IllegalArgumentException();
return IntStream
.iterate(0, x -> x+1)
.map(x -> x % (int)Math.pow(2, n))
.map(x -> x^(x>>1));

}

Expand Down
23 changes: 22 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,26 @@ public static <T> void findMinMax(
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {

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

if(iter.hasNext())
{
T min = iter.next();
T max = min;

while (iter.hasNext())
{
T element = iter.next();

if (order.compare(element, max) >= 0)
max = element;
else if(order.compare(element, min) < 0 )
min = element;
}
minMaxConsumer.accept(min, max);
return;
}

minMaxConsumer.accept(null, null);
}
}
27 changes: 24 additions & 3 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
package com.example.task04;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Map;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Task04Main {
public class Task04Main
{

public static void main(String[] args) {
public static void main(String[] args)
{
Stream<String> stream = new BufferedReader(new InputStreamReader(System.in)).lines();

// your implementation here
stream
.map(String::toLowerCase)
.flatMap(x -> Stream.of(x.split("[^a-zа-яё0-9]")))
.filter(x -> !x.isEmpty())
.collect(Collectors.groupingBy(n -> n, Collectors.counting()))
.entrySet()
.stream()
.sorted(Comparator.comparingLong((ToLongFunction<Map.Entry<String, Long>>) Map.Entry::getValue)
.reversed()
.thenComparing(Map.Entry::getKey))
.limit(10)
.forEach(x -> System.out.print(x.getKey() + '\n'));

}

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

public abstract class AbstractMessage<T> {

private final String from;

public final String getFrom() {
return from;
}

private final String to;

public final String getTo() {
return to;
}

public abstract T getContent();

public AbstractMessage(String from, String to) {
this.to = to;
this.from = from;
}
}
16 changes: 16 additions & 0 deletions task05/src/com/example/task05/MailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.task05;

public class MailMessage extends AbstractMessage<String> {

private final String content;

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

public MailMessage(String from, String to, String message) {
super(from, to);
this.content = message;
}
}
37 changes: 37 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.task05;

import java.util.function.Consumer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class MailService<T> implements Consumer {

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

return super.get(key);
}
};

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

public MailService() {

}

@Override
public void accept(Object o) {
AbstractMessage message = (AbstractMessage) o;
mailBox.putIfAbsent(message.getTo(), new ArrayList<>());
mailBox.computeIfAbsent(message.getTo(), k -> new ArrayList<>());
mailBox.get(message.getTo()).add((T) message.getContent());
}
}
16 changes: 16 additions & 0 deletions task05/src/com/example/task05/Salary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.task05;

public class Salary extends AbstractMessage<Integer> {

private final int content;

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

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

public class Task05Main {

public static void main(String[] args) {
public static void main(String[] args)
{


/*

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


*/


}

Expand Down