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
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
import scala.compiletime.ops.boolean
|
||||||
|
|
||||||
|
class Problem12Suite extends munit.FunSuite:
|
||||||
|
List(
|
||||||
|
(List((1 ,1), (1, 2), (1, 3), (1, 4)), List(1, 2, 3, 4)),
|
||||||
|
(List(), List()),
|
||||||
|
(List((1, 1), (1, 2), (1, 3), (1, 2)), List(1, 2, 3, 2)),
|
||||||
|
(List((1, 1)), List(1)),
|
||||||
|
(List((2, 1)), List(1, 1)),
|
||||||
|
(List((4, 1), (1,2), (2, 3), (2, 1), (1, 4), (4, 5)), List(1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5))
|
||||||
|
).foreach:
|
||||||
|
case (list, expected) =>
|
||||||
|
test(s"runLengthDecode returns [$expected]"):
|
||||||
|
assertEquals(Problem12.runLengthDecode(list), expected)
|
||||||
Reference in New Issue
Block a user