Skip to content

Instantly share code, notes, and snippets.

View potea's full-sized avatar

Jaewook Yu potea

View GitHub Profile
@potea
potea / look_and_say_sequence_kotlin_coroutine.kt
Created April 6, 2017 21:04
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")
@potea
potea / look_and_say_sequence_kotlin_list.kt
Created April 6, 2017 21:02
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)
}
}