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
2 changes: 1 addition & 1 deletion task01/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Задание 01 ternaryOperator

Давайте научимся комбинировать функции в более сложные функции.
Для примера построим следующую комбинацию.
Для примера построим следующую комбинацию.
Дан предикат condition и две функции ifTrue и ifFalse.
Напишите метод ternaryOperator, который из них построит новую функцию,
возвращающую значение функции ifTrue, если предикат выполнен, и значение ifFalse иначе.
Expand Down
10 changes: 6 additions & 4 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,22 @@ 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
if (condition == null || ifTrue == null || ifFalse == null)
throw new NullPointerException();
return (x) -> condition.test(x) ? ifTrue.apply(x) : ifFalse.apply(x); // your implementation here

}
}
12 changes: 8 additions & 4 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ 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++;
return (int) (a % Math.pow(2, n));
}).map(a -> a ^ (a >> 1)); // your implementation here

}

Expand Down
18 changes: 16 additions & 2 deletions 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 @@ -21,7 +22,20 @@ public static <T> void findMinMax(
Stream<? extends T> stream,
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);
}
}
24 changes: 21 additions & 3 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +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.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"));
}

}
15 changes: 15 additions & 0 deletions task05/src/com/example/task05/InfoKeeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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);
}
}
28 changes: 28 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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> tInfoKeeper) {
if (MailBox.containsKey(tInfoKeeper.getTo())) {
MailBox.get(tInfoKeeper.getTo()).add(tInfoKeeper.getContent());
} else {
ArrayList<T> arrayList = new ArrayList<>();
arrayList.add(tInfoKeeper.getContent());
MailBox.put(tInfoKeeper.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 from, String to, Integer content) {
super(from, to, 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