Problem 09

This commit is contained in:
2026-03-19 15:39:22 +01:00
parent 659d211d8c
commit 7e6ea14b44
2 changed files with 120 additions and 0 deletions
@@ -0,0 +1,66 @@
package ninetyNineScalaProblems
import scala.annotation.tailrec
object Problem09:
def sameValueRepeatedAnyTime(list: List[Any]): Boolean =
@tailrec
def loop(cursor: List[Any], acc: Boolean): Boolean =
cursor match
case List(x) =>
true
case x1 :: List(x2) if x1 == x2 =>
true
case x1 :: x2 :: tail if x1 == x2 =>
loop(x1 :: tail, true)
case x :: tail =>
false
case Nil =>
true
loop(list, true)
def countRepititions(list: List[Any]): Int =
@tailrec
def loop(cursor: List[Any], acc: Int): Int =
cursor match
case e1 :: e2 :: Nil if e1 == e2 =>
acc + 2
case e1 :: e2 :: tail if e1 == e2 =>
loop(e2 :: tail, acc + 1)
case e :: tail =>
acc + 1
case Nil =>
acc
loop(list, 0)
def nthElementToEnd(list: List[Any], n: Int): List[Any] =
@tailrec
def loop(cursor: List[Any], acc: List[Any], position: Int): List[Any] =
cursor match
case e :: tail if position >= n =>
loop(tail, e :: acc, position + 1)
case e :: tail =>
loop(tail, acc, position + 1)
case Nil =>
acc
loop(list, List(), 0).reverse
def consecutiveDuplicatesIntoSublists(list: List[Any]): List[List[Any]] =
@tailrec
def loop(cursor: List[Any], acc: List[List[Any]]): List[List[Any]] =
val repititions = countRepititions(cursor)
cursor match
case Nil =>
acc
case _ if repititions == 0 =>
acc
case _ => cursor match
case e :: tail =>
loop(nthElementToEnd(tail, repititions - 1), List.fill(repititions)(e) :: acc)
case Nil =>
acc
loop(list, List()).reverse
@@ -0,0 +1,54 @@
package ninetyNineScalaProblems
import scala.compiletime.ops.boolean
class Problem09Suite extends munit.FunSuite:
List(
(List(1, 2, 3, 4), false),
(List(1, 2), false),
(List(1, 1), true),
(List(), true),
(List(1), true),
).foreach:
case (list, expected) =>
test(s"sameValueRepeatedAnyTime returns [$expected]"):
assertEquals(Problem09.sameValueRepeatedAnyTime(list), expected)
List(
(List(1, 2, 3, 4), 1),
(List(1, 1, 2, 3, 4), 2),
(List(2, 1, 2, 3, 4), 1),
(List(2, 1, 2, 2, 2), 1),
(List(2, 2, 2, 2, 2), 5),
(List(2, 3, 2, 2, 2), 1),
(List(1, 2), 1),
(List(1, 1), 2),
(List(), 0),
(List(1), 1),
).foreach:
case (list, expected) =>
test(s"countRepititions returns [$expected]"):
assertEquals(Problem09.countRepititions(list), expected)
List(
((List(1, 2, 3, 4), 0), List(1, 2, 3, 4)),
((List(1, 2, 3, 4), 1), List(2, 3, 4)),
((List(1, 2), 1), List(2)),
((List(1, 2), 2), List()),
((List(), 0), List()),
).foreach:
case ((list, n), expected) =>
test(s"nthElementToEnd returns [$expected]")
assertEquals(Problem09.nthElementToEnd(list, n), expected)
List(
(List(1, 2, 3, 4), List(List(1), List(2), List(3), List(4))),
(List(1, 2, 2, 3), List(List(1), List(2, 2), List(3))),
(List(1, 2, 3, 2), List(List(1), List(2), List(3), List(2))),
(List(), List()),
(List(1), List(List(1))),
(List(1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5), List(List(1, 1, 1, 1), List(2), List(3, 3), List(1, 1), List(4), List(5, 5, 5, 5))),
).foreach:
case (list, expected) =>
test(s"consecutiveDuplicatesIntoSublists returns [$expected]"):
assertEquals(Problem09.consecutiveDuplicatesIntoSublists(list), expected)