August 26, 2024

P25 - Generate a random permutation of the elements of a list.

Make sure there is a way to produce deterministic results. Hint: Use the solution of problem P23. Example:

> randomPermute("abcdef".toList())
[d, b, e, f, a, c]

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> randomPermute(list: List<T>, random: Random = Random): List<T> =
    randomSelect(list.size, list, random)

class P25Test {
    @Test fun `generate a random permutation of the elements of a list`() {
        assertThat(randomPermute("abcdef".toList(), Random(seed = 123)), equalTo("edbfac".toList()))
    }
}
Be first to comment
Leave a reply