-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP28.java
More file actions
60 lines (53 loc) · 1.71 KB
/
P28.java
File metadata and controls
60 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package lists;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.toList;
/**
* We suppose that a list (InList) contains elements that are lists themselves.
* The objective is to sort the elements of InList according to their length.
* E.g. short lists first, longer lists later, or vice versa.
* <p>
* Example:
* ?- lsort([[a,b,c],[d,e],[f,g,h],[d,e],[i,j,k,l],[m,n],[o]],L).
* L = [[o], [d, e], [d, e], [m, n], [a, b, c], [f, g, h], [i, j, k, l]]
*/
public final class P28 {
private P28() {
}
/**
* Sort list by size of sublist.
*
* @param input input list of items
* @param <T> type of item
* @return sorted list
*/
public static <T> List<List<T>> lsort(final List<List<T>> input) {
input.sort(Comparator.comparingInt(List::size));
return input;
}
/**
* Sort list by frequency of sublist.
*
* @param input input list of items
* @param <T> type of item
* @return sorted list
*/
public static <T> List<List<T>> lfsort(final List<List<T>> input) {
Map<Integer, Integer> frequencies = new HashMap<>();
input.stream()
.map(List::size)
.forEach(l -> frequencies.put(
l,
frequencies.compute(l,
(k, v) -> v == null
? 1
: v + 1)));
return input
.stream()
.sorted(Comparator
.comparingInt(xs -> frequencies.get(xs.size())))
.collect(toList());
}
}