Problem 12
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user