Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Look and say sequence in Kotlin
fun ant2(count: Int) {
fun lookAndSay(sequence: Sequence<String>): Sequence<String> {
return buildSequence {
var head = '1'
var count = 0
sequence.forEach {
for (c in it) {
if (c != head) {
yield("$head$count")
count = 1
head = c
} else {
count++
}
}
}
yield("$head$count")
}
}
var sequence: Sequence<String> = sequenceOf("1")
val antSeq = buildSequence {
while (true) {
yield(sequence)
sequence = lookAndSay(sequence)
}
}
antSeq.take(count).last().forEach(::print)
println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment