Problem 13
This commit is contained in:
@@ -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
|
||||
@@ -0,0 +1,16 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.compiletime.ops.boolean
|
||||
|
||||
class Problem13Suite extends munit.FunSuite:
|
||||
List(
|
||||
(List(1, 2, 3, 4), List((1, 1), (1, 2), (1, 3), (1, 4))),
|
||||
(List(1, 2, 2, 3), List((1, 1), (2, 2), (1, 3))),
|
||||
(List(1, 2, 3, 2), List((1, 1), (1, 2), (1, 3), (1, 2))),
|
||||
(List(), List()),
|
||||
(List(1, 1, 1), List((3, 1))),
|
||||
(List(1, 1, 1, 2), List((3, 1), (1, 2))),
|
||||
).foreach:
|
||||
case (list, expected) =>
|
||||
test(s"runLengthEncoding returns [$expected]"):
|
||||
assertEquals(Problem13.runLengthEncode(list), expected)
|
||||
Reference in New Issue
Block a user