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
13 changes: 5 additions & 8 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package com.example.task01;

import java.io.IOException;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

public class Task01Main {
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 || ifFalse == null || ifTrue == null){ // проверка на null
throw new NullPointerException();
}
return x -> condition.test(x) ? ifTrue.apply(x) : ifFalse.apply(x);
}
}
14 changes: 7 additions & 7 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
public class Task02Main {

public static void main(String[] args) {

/*
cycleGrayCode(2)
.limit(10)
.forEach(System.out::println);
*/
}

public static int GetGrayCode(int g){
return g ^ (g >> 1); // формула с википедии
}

public static IntStream cycleGrayCode(int n) {

return null; // your implementation here

if (n >= 1 && n <= 16){ // Битность кодов задается аргументом n в пределах от 1 до 16.
return IntStream.iterate(0, g -> g + 1).map(g -> GetGrayCode(g % (int)Math.pow(2, n)));
}
throw new IllegalArgumentException(); // ошибка, если n не лежит в [1, 16]
}

}
20 changes: 14 additions & 6 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.task03;

import java.util.Comparator;
import java.util.Iterator;
import java.util.function.BiConsumer;
import java.util.stream.Stream;

public class Task03Main {

public static void main(String[] args) {

findMinMax(
Stream.of(2, 9, 5, 4, 8, 1, 3),
Integer::compareTo,
Expand All @@ -17,11 +17,19 @@ public static void main(String[] args) {

}

public static <T> void findMinMax(
Stream<? extends T> stream,
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {
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(); // итератор для обхода стрима
// Берем первый элемент из итератора, если он есть. Иначе - null
T element = iterator.hasNext() ? iterator.next() : null;
T max = element;
T min = element;
while(iterator.hasNext()) {
element = iterator.next();
min = order.compare(element, min) < 0 ? element : min;
max = order.compare(element, max) > 0 ? element : max;
}
minMaxConsumer.accept(min, max);
}
}
23 changes: 22 additions & 1 deletion task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package com.example.task04;

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

public class Task04Main {

public static void main(String[] args) {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

buffer.lines()
.flatMap(x -> Stream.of(x.split("\\P{L}")) // разделяем по всем символам, которые не буквы
.filter(t -> !t.isEmpty())
.map(String::toLowerCase))

.collect(Collectors.groupingBy(t -> t, Collectors.counting()))
.entrySet()
.stream()

// your implementation here
.sorted(Map.Entry.comparingByKey())
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))

.limit(10)
.map(Map.Entry::getKey)
.forEach(t -> System.out.print(t + '\n'));
}

}
7 changes: 0 additions & 7 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
public class Task05Main {

public static void main(String[] args) {

/*

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


*/

}

}