Created
April 6, 2017 21:02
-
-
Save potea/6238105562a722adc555f59c288f53fa to your computer and use it in GitHub Desktop.
Look and say sequence in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ant1(count: Int) { | |
fun lookAndSay(list: List<Int>): List<Int> { | |
val count = list.takeWhile { it == list[0] }.size | |
val sublist = list.drop(count) | |
return when(sublist.size) { | |
0 -> listOf(list[0], count) | |
else -> listOf(list[0], count) + lookAndSay(sublist) | |
} | |
} | |
var list: List<Int> = listOf(1) | |
for (x in 1 until count) { | |
list = lookAndSay(list) | |
} | |
list.forEach(::print) | |
println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment