P13 { public static List<SimpleEntry<Integer, String>> encode_direct(List<String> list) { LinkedList<SimpleEntry<Integer, String>> result = new LinkedList<>(); String lastElem = null; for (String elem : list) { if (Objects.equals(lastElem, elem)) { SimpleEntry<Integer, String> last = result.removeLast(); result.add(new SimpleEntry<>(last.getKey() + 1, elem)); } else { result.add(new SimpleEntry<>(1, elem)); lastElem = elem; } } return result; }  static List<SimpleEntry<Integer, String>> encode_direct(List<String> list);  }
@Test public void shouldEncodeAList() throws Exception { List<SimpleEntry<Integer, String>> encodedList = P13.encode_direct(Arrays.asList("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e")); assertThat(encodedList, hasSize(6)); assertThat(encodedList.get(0), is(equalTo(new SimpleEntry<>(4, "a")))); assertThat(encodedList.get(1), is(equalTo(new SimpleEntry<>(1, "b")))); assertThat(encodedList.get(2), is(equalTo(new SimpleEntry<>(2, "c")))); assertThat(encodedList.get(3), is(equalTo(new SimpleEntry<>(2, "a")))); assertThat(encodedList.get(4), is(equalTo(new SimpleEntry<>(1, "d")))); assertThat(encodedList.get(5), is(equalTo(new SimpleEntry<>(4, "e")))); }