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
7 changes: 4 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,10 @@ 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 +22,9 @@ 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: 4 additions & 3 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ 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 < 1 || n > 16)
throw new IllegalArgumentException();
return IntStream.iterate(0, a -> a >= Math.pow(2, n) - 1 ? 0 : a + 1)
.map(b -> b ^ (b >> 1));

}

Expand Down
21 changes: 20 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,24 @@ 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 min = null;
T max = null;
if (iterator.hasNext()){
min = iterator.next();
max = min;
}

while (iterator.hasNext()){
T currentElement = iterator.next();
if (order.compare(currentElement,max)>0){
max = currentElement;
}
if (order.compare(currentElement,min)<0){
min = currentElement;
}
}
minMaxConsumer.accept(min,max);
}
}
23 changes: 21 additions & 2 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package com.example.task04;

import java.io.*;
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 static void main(String[] args) {

// your implementation here
// System.setIn(new ByteArrayInputStream("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sodales consectetur purus at faucibus. Donec mi quam, tempor vel ipsum non, faucibus suscipit massa. Morbi lacinia velit blandit tincidunt efficitur. Vestibulum eget metus imperdiet sapien laoreet faucibus. Nunc eget vehicula mauris, ac auctor lorem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel odio nec mi tempor dignissim.".getBytes()));

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

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

public class InfoKeeper<T> {
private String from;
private String to;
private T content;

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

public String getFrom () { return from; }
public String getTo () { return to; }
public T getContent () { return content; }
}
7 changes: 7 additions & 0 deletions task05/src/com/example/task05/MailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.task05;

public class MailMessage extends InfoKeeper<String> {
public MailMessage(String to, String from, String content) {
super(to, from, content);
}
}
31 changes: 31 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.task05;

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

public class MailService<T> implements Consumer<InfoKeeper<T>> {
private final Map<String, List<T>> MailBox = new HashMap<String, List<T>>(){
@Override
public List<T> get(Object key) {
List<T> list = super.get(key);
return list != null ? list : new ArrayList<>();
}
};
public Map<String, List<T>> getMailBox() {
return MailBox;
}

@Override
public void accept(InfoKeeper<T> baseObjClass){
if (MailBox.containsKey(baseObjClass.getTo())) {
MailBox.get(baseObjClass.getTo()).add(baseObjClass.getContent());
} else {
ArrayList<T> arrayList = new ArrayList<>();
arrayList.add(baseObjClass.getContent());
MailBox.put(baseObjClass.getTo(), arrayList);
}
}
}
7 changes: 7 additions & 0 deletions task05/src/com/example/task05/Salary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.task05;

public class Salary extends InfoKeeper<Integer> {
public Salary(String to, String from, int content) {
super(to, from, content);
}
}
2 changes: 0 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,6 @@ public class Task05Main {

public static void main(String[] args) {

/*

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


*/

}

Expand Down