-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP13.java
More file actions
41 lines (35 loc) · 1.13 KB
/
P13.java
File metadata and controls
41 lines (35 loc) · 1.13 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
package lists;
import java.util.AbstractMap.SimpleEntry;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
* Implement the so-called run-length encoding data compression method directly.
* I.e. don't explicitly create the sublists containing the duplicates,
* as in problem 1.09, but only count them. As in problem 1.11,
* simplify the result list by replacing the singleton terms [1,X] by X.
* Example:
* ?- encode_direct([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
* X = [[4,a],b,[2,c],[2,a],d,[4,e]]
*/
public final class P13 {
private P13() {
}
/**
* Encode direct all items.
*
* @param list list of items
* @param <T> type of item
* @return list of simple entries
*/
@SuppressWarnings("unchecked")
public static <T> List<SimpleEntry<Integer, T>> encodeDirect(
final List<String> list) {
return P09.pack(list)
.stream()
.map(sublist ->
new SimpleEntry<>(
sublist.size(),
(T) sublist.get(0)))
.collect(toList());
}
}