Problem 13

This commit is contained in:
jakob.scheid committed 2026-03-19 15:39:28 +01:00
1 parent 13487dcb42
commit f1be55948f
2 files changed
+35

No files matched your search

@@ -0,0 +1,19 @@
package ninetyNineScalaProblems
import scala.annotation.tailrec
object Problem13:
def runLengthEncode(list: List[Any]): List[Tuple2[Int, Any]] =
@tailrec
def loop(cursor: List[Any], acc: List[Tuple2[Int, Any]]): List[Tuple2[Int, Any]] =
cursor match
case e :: tail =>
acc match
case (f, v) :: t if v == e =>
loop(tail, (f + 1, v) :: t)
case a =>
loop(tail, (1, e) :: a)
case Nil =>
acc
loop(list, List()).reverse