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);
}

}
13 changes: 9 additions & 4 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.example.task02;

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Task02Main {

public static void main(String[] args) {

/*
cycleGrayCode(2)

cycleGrayCode(3)
.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();
}
return IntStream.iterate(0, t -> (t + 1) % (int)Math.pow(2, n)
).map(f -> f ^ (f >> 1)); // your implementation here

}

Expand Down
28 changes: 27 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.List;
import java.util.TreeSet;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Task03Main {
Expand All @@ -21,7 +25,29 @@ public static <T> void findMinMax(
Stream<? extends T> stream,
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {
if (stream == null || order == null || minMaxConsumer == null) {
throw new NullPointerException();
}

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

T maxElement = null;
T minElement = null;

if(iterator.hasNext())
{ T firstElement = iterator.next();
maxElement = firstElement;
minElement = firstElement;
}

while (iterator.hasNext()){
T currentElement = iterator.next();
if (order.compare(currentElement,maxElement)>0)
maxElement = currentElement;
if (order.compare(currentElement,minElement)<0)
minElement = currentElement;
}

minMaxConsumer.accept(minElement,maxElement);
}
}
23 changes: 22 additions & 1 deletion 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 static void main(String[] args) {

// your implementation here
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"));

}

Expand Down
26 changes: 26 additions & 0 deletions task05/src/com/example/task05/InfoKeeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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);
}
}
23 changes: 23 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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> keeper) {
MailBox.computeIfAbsent(keeper.getTo(), k -> new ArrayList<>())
.add(keeper.getContent());
}
}
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, Integer content){
super(to, from, content);
}
}
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