Skip to content
Open

gg #506

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
21 changes: 16 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,12 +10,12 @@ 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 +24,17 @@ 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("One of the parameters is null");
}

return input -> {
if (condition.test(input)){
return ifTrue.apply(input);
}
else{
return ifFalse.apply(input);
}
};
}
}
}
14 changes: 10 additions & 4 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ public class Task02Main {

public static void main(String[] args) {

/*

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


}
// Число сдвигаем побитово вправо на 1. И исключающее или между исходным и получившимся числом

public static IntStream cycleGrayCode(int n) {
if (n < 1 || n > 16){
throw new IllegalArgumentException("Неправильное n");
}

return null; // your implementation here
int limit = 1 << n; // 2 в степени n
return IntStream.iterate(0, i -> (i + 1) % limit)
.map(i -> i ^ (i >> 1)); // Преобразование в код грея

}

}
}
35 changes: 32 additions & 3 deletions 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.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Task03Main {
Expand All @@ -20,8 +24,33 @@ 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) {
BiConsumer<? super T, ? super T> minMaxConsumer)
{
if (stream == null || order == null || minMaxConsumer == null) {
throw new NullPointerException();
}


Iterator<? extends T> iterator = stream.iterator();
if (!iterator.hasNext()) {
minMaxConsumer.accept(null, null);
return;
}

T first = iterator.next();
T min = first;
T max = first;

while (iterator.hasNext()) {
T element = iterator.next();
if (order.compare(element, min) < 0) {
min = element;
}
if (order.compare(element, max) > 0) {
max = element;
}
}

// your implementation here
minMaxConsumer.accept(min, max);
}
}
}
33 changes: 27 additions & 6 deletions 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;

public class Task04Main {

public static void main(String[] args) {
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

// your implementation here
public class Task04Main {

public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, StandardCharsets.UTF_8)
)) {
reader.lines()
.map(String::toLowerCase)
.flatMap(line -> Arrays.stream(line.split("[^a-zа-яё0-9]+")))
.filter(s -> !s.isEmpty())
.collect(Collectors.groupingBy(word -> word, Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.<String, Long>comparingByValue()
.reversed()
.thenComparing(Map.Entry.comparingByKey()))
.limit(10)
.map(Map.Entry::getKey)
.forEach(word -> System.out.print(word + "\n"));
}
}

}
}
97 changes: 91 additions & 6 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.example.task05;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;

public class Task05Main {

public static void main(String[] args) {

/*

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


*/


}

}

interface Sendable<T>{
String getFrom();
String getTo();
T getContent();
}

class MailMessage implements Sendable<String>{
private String from;
private String to;

private String content;

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

public String getFrom(){
return from;
}

public String getTo(){
return to;
}

public String getContent(){
return content;
}

}

class Salary implements Sendable<Integer>{
private Integer salary;
private String from;
private String to;
public Salary(String from, String to, Integer salary){
this.salary = salary;
this.from = from;
this.to = to;
}

public Integer getContent(){
return salary;
}

public String getFrom(){
return from;
}
public String getTo(){
return to;
}
}

class MailService<T> implements Consumer<Sendable<T>>{
private Map<String, List<T>> mailbox = new HashMap<>();

public void accept(Sendable<T> sendable){
if (mailbox.containsKey(sendable.getTo())){
List<T> temp = mailbox.get(sendable.getTo());
temp.add(sendable.getContent());
mailbox.replace(sendable.getTo(), temp);
}
else{
List<T> temp = new ArrayList<>();
temp.add(sendable.getContent());
mailbox.put(sendable.getTo(), temp);
}
}

public Map<String, List<T>> getMailBox(){
return new AbstractMap<String, List<T>>() {
@Override
public Set<Entry<String, List<T>>> entrySet() {
return mailbox.entrySet();
}

@Override
public List<T> get(Object key) {
List<T> value = mailbox.get(key);
return value == null ? Collections.emptyList() : value;
}
};
}


}