fun stringElements3(s: String, list: List<Int>): String =
buildString {
list.filter { it % 2 == 0 }
.forEach { append(s[it]) }
}
Function Call | Return Value | |||
---|---|---|---|---|
stringElements3("abcdefgh", listOf(3, 2, 1, 0)) | → | |||
stringElements3("hello there", listOf(0, 1, 2, 4)) | → | |||
stringElements3("This is a test", listOf(4, 2, 2, 0)) | → | |||
stringElements3("How was your day?", listOf(0, 4, 4, 3, 7, 7)) | → | |||
stringElements3("Welcome to ReadingBat", listOf(1, 4, 2, 6, 8)) | → |
Experiment with this code on Gitpod.io or as a Kotlin Playground