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 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") |
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) | |
} | |
} |