August 26, 2024

P19 - Rotate a list N places to the left.

Examples:

> rotate(3, "abcdefghijk".toList())
[d, e, f, g, h, i, j, k, a, b, c]

> rotate(-2, "abcdefghijk".toList())
[j, k, a, b, c, d, e, f, g, h, i]

kotlin

package org.kotlin99.lists

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

fun <T> rotate(n: Int, list: List<T>): List<T> =
    when {
        n == 0 -> list
        n > 0  -> list.drop(n) + list.take(n)
        else   -> list.takeLast(-n) + list.dropLast(-n)
    }

class P19Test {
    @Test fun `rotate a list N places to the left`() {
        assertThat(rotate(3, "abcdefghijk".toList()), equalTo("defghijkabc".toList()))
        assertThat(rotate(-2, "abcdefghijk".toList()), equalTo("jkabcdefghi".toList()))
    }
}
Be first to comment
Leave a reply