August 26, 2024

P23 - Extract a given number of randomly selected elements from a list.

Make sure there is a way to produce deterministic results. Example:

> randomSelect(3, "abcdefgh".toList())
[c, h, f]

kotlin

package org.kotlin99.lists

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test
import kotlin.random.Random

fun <T> randomSelect(n: Int, list: List<T>, random: Random = Random): List<T> =
    if (n == 0) emptyList()
    else {
        val value = list[random.nextInt(list.size)]
        randomSelect(n - 1, list.filter { it != value }, random) + value
    }

class P23Test {
    @Test fun `extract a given number of randomly selected elements from a list`() {
        assertThat(randomSelect(3, "abcdefgh".toList(), Random(seed = 123)), equalTo("gfa".toList()))
    }
}
Be first to comment
Leave a reply