August 26, 2024
P13 - Run-length encoding of a list (direct solution).
Implement the so-called run-length encoding data compression method directly. I.e. don't use other methods you've written (like P09's pack); do all the work directly. Example:
> encodeDirect("aaaabccaadeeee".toList())
[(4, a), (1, b), (2, c), (2, a), (1, d), (4, e)]
kotlin
package org.kotlin99.lists
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test
fun <T> encodeDirect(list: List<T>): List<Pair<Int, T>> =
list.fold(emptyList()) { result, value ->
if (result.isEmpty()) listOf(Pair(1, value))
else {
val last = result.last()
if (last.second == value) result.dropLast(1) + Pair(last.first + 1, value)
else result + Pair(1, value)
}
}
class P13Test {
@Test fun `run-length encoding of a list (direct solution)`() {
assertThat(encodeDirect("aaaabccaadeeee".toList()), equalTo(listOf(
Pair(4, 'a'), Pair(1, 'b'), Pair(2, 'c'), Pair(2, 'a'), Pair(1, 'd'), Pair(4, 'e')
)))
}
}