Problem 12

This commit is contained in:
2026-03-19 15:39:26 +01:00
parent 451a728117
commit 13487dcb42
2 changed files with 35 additions and 0 deletions
@@ -0,0 +1,19 @@
package ninetyNineScalaProblems
import scala.annotation.tailrec
import scala.annotation.constructorOnly
object Problem12:
def runLengthDecode(list: List[Tuple2[Int, Any]]): List[Tuple2[Int, Any] | Any] =
@tailrec
def loop(cursor: List[Tuple2[Int, Any]], acc: List[Tuple2[Int, Any] | Any]): List[Tuple2[Int, Any] | Any] =
cursor match
case t :: tail =>
t match
case (f, v) if f > 1 =>
loop((f - 1, v) :: tail, v :: acc)
case (f, v) =>
loop(tail, v :: acc)
case Nil => acc
loop(list, List()).reverse