forked from dkandalov/kotlin-99
-
Notifications
You must be signed in to change notification settings - Fork 1
/
P11.kt
32 lines (26 loc) · 978 Bytes
/
P11.kt
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
package org.kotlin99.lists
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test
/*
P11 (*) Modified run-length encoding.
Modify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result
list. Only elements with duplicates are transferred as (N, E) terms. Example:
> encodeModified("aaaabccaadeeee".toList())
[(4, a), b, (2, c), (2, a), d, (4, e)]
*/
@Suppress("IMPLICIT_CAST_TO_ANY")
fun <T : Any> encodeModifies(list: List<T>): List<Any> =
pack(list).map {
when (it.count()) {
1 -> it.first()
else -> Pair(it.count(), it.first())
}
}
class P11Test {
@Test fun `modified run-length encoding`() {
assertThat(encodeModifies("aaaabccaadeeee".toList()), equalTo(listOf<Any>(
Pair(4, 'a'), 'b', Pair(2, 'c'), Pair(2, 'a'), 'd', Pair(4, 'e')
)))
}
}