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
33 changes: 21 additions & 12 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
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 должен компилироваться и успешно работать следующий код:

/*
public class Task01Main
{
public static void main(String[] args) throws IOException
{
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

Function<? super T, ? extends U> ifFalse)
{
if(condition == null || ifTrue == null || ifFalse == null)
{
throw new NullPointerException();
}

return obj ->
{
if(condition.test(obj))
{
return ifTrue.apply(obj);
}

return ifFalse.apply(obj);
};
}
}
23 changes: 12 additions & 11 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

import java.util.stream.IntStream;

public class Task02Main {

public static void main(String[] args) {

/*
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
public static IntStream cycleGrayCode(int n)
{
if (n < 1 || n > 16 )
{
throw new IllegalArgumentException();
}

return IntStream.iterate(0, x -> x < Math.pow(2, n) - 1 ? x + 1 : 0)
.map(x -> x ^ (x >> 1));
}

}
47 changes: 40 additions & 7 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
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) {

public class Task03Main
{
public static void main(String[] args)
{
findMinMax(
Stream.of(2, 9, 5, 4, 8, 1, 3),
Integer::compareTo,
(min, max) ->
System.out.println("min: " + min + " / max: " + max)
);

}

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

else
{
T min = iterator.next();
T max = min;

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

else if (order.compare(current, max) > 0)
{
max = current;
}
}

// your implementation here
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,10 +1,28 @@
package com.example.task04;

public class Task04Main {
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public static void main(String[] args) {
public class Task04Main {

// your implementation here
public static void main(String[] args)
{
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(Map.Entry.comparingByKey())
.sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
.limit(10)
.forEach(e -> System.out.print(e.getKey() + "\n"));

}

Expand Down
7 changes: 7 additions & 0 deletions task05/src/com/example/task05/IMail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.task05;

public interface IMail<T> {
String getFrom();
String getTo();
T getContent();
}
31 changes: 31 additions & 0 deletions task05/src/com/example/task05/MailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.task05;

public class MailMessage implements IMail<String>
{
private String Content;
private String to;
private String from;

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

public String getContent()
{
return Content;
}


public String getTo()
{
return to;
}

public String getFrom()
{
return from;
}
}
27 changes: 27 additions & 0 deletions task05/src/com/example/task05/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.task05;

import java.util.*;
import java.util.function.Consumer;

public class MailService<T> implements Consumer<IMail<T>>
{
private final Map<String, List<T>> mailBox = new HashMap<String, List<T>>()
{
@Override
public List<T> get(Object key)
{
return super.getOrDefault(key, Collections.emptyList());
}
};

@Override
public void accept(IMail<T> mail)
{
mailBox.computeIfAbsent(mail.getTo(), k -> new ArrayList<>()).add(mail.getContent());
}

public Map<String, List<T>> getMailBox()
{
return mailBox;
}
}
30 changes: 30 additions & 0 deletions task05/src/com/example/task05/Salary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.task05;

public class Salary implements IMail<Integer>
{
String companyName;
String to;
int salary;

Salary(String companyName, String name, int salary)
{
this.companyName = companyName;
this.to = name;
this.salary = salary;
}

public String getFrom()
{
return companyName;
}

public String getTo()
{
return to;
}

public Integer getContent()
{
return salary;
}
}
5 changes: 0 additions & 5 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public class Task05Main {

public static void main(String[] args) {

/*

// Random variables
String randomFrom = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
String randomTo = "..."; // Некоторая случайная строка. Можете выбрать ее самостоятельно.
Expand Down Expand Up @@ -87,9 +85,6 @@ public static void main(String[] args) {
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)";


*/

}

}