Skip to content

Instantly share code, notes, and snippets.

@potea
Created April 6, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potea/6238105562a722adc555f59c288f53fa to your computer and use it in GitHub Desktop.
Save potea/6238105562a722adc555f59c288f53fa to your computer and use it in GitHub Desktop.
Look and say sequence in Kotlin
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