August 26, 2024

P12 - Decode a run-length encoded list.

Given a run-length code list generated as specified in the problem P10, construct its uncompressed version. Example:

> decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e)))
[a, a, a, a, b, c, c, a, a, d, e, e, e, e]

kotlin

package org.kotlin99.lists

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test

fun <T> decode(list: List<Pair<Int, T>>): List<T> = list.flatMap { (count, value) -> List(count) { value } }

class P12Test {
    @Test fun `decode a run-length encoded list`() {
        assertThat(decode(listOf(
            Pair(4, 'a'), Pair(1, 'b'), Pair(2, 'c'), Pair(2, 'a'), Pair(1, 'd'), Pair(4, 'e')
        )), equalTo("aaaabccaadeeee".toList()))
    }
}
Be first to comment
Leave a reply