Problem 07

This commit is contained in:
2026-03-19 15:39:19 +01:00
parent 79fa83ef67
commit 78f56c809e
2 changed files with 26 additions and 0 deletions
@@ -0,0 +1,12 @@
package ninetyNineScalaProblems
import scala.annotation.tailrec
object Problem07:
def flattenNestedList(list: List[Any]): List[Any] =
def loop(l: List[Any]): List[Any] =
l.flatMap:
case ls: List[Any] => loop(ls)
case e => List(e)
loop(list)
@@ -0,0 +1,14 @@
package ninetyNineScalaProblems
class Problem07Suite extends munit.FunSuite:
List(
(List(1, List(2, 3), List(List(4))), List(1, 2, 3, 4)),
(List(List()), List()),
(List(1, List(2, 3)), List(1, 2, 3)),
(List(0, 0, 0), List(0, 0, 0)),
(List(1), List(1)),
(List(), List())
).foreach:
case (list, expected) =>
test(s"flattenNestedList returns [$expected]"):
assertEquals(Problem07.flattenNestedList(list), expected)