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