Skip to content

Instantly share code, notes, and snippets.

@potea
Created April 6, 2017 21:04
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/f27017ea5094b86365fa07065628fb71 to your computer and use it in GitHub Desktop.
Save potea/f27017ea5094b86365fa07065628fb71 to your computer and use it in GitHub Desktop.
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