Problem 05
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Problem05:
|
||||
def reverseList(list: List[Any]): List[Any] =
|
||||
@tailrec
|
||||
def loop(cursor: List[Any], acc: List[Any]): List[Any] =
|
||||
cursor match
|
||||
case head :: tail =>
|
||||
loop(tail, head :: acc)
|
||||
case Nil =>
|
||||
acc
|
||||
|
||||
loop(list, List())
|
||||
|
||||
object Problem05Stdlib:
|
||||
def reverseList(list: List[Any]): List[Any] =
|
||||
list.reverse
|
||||
@@ -0,0 +1,13 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
class Problem05Suite extends munit.FunSuite:
|
||||
List(
|
||||
(List(1, 2, 3, 4, 5), List(5, 4, 3, 2, 1)),
|
||||
(List(1), List(1)),
|
||||
(List(), List()),
|
||||
(List(0, 0, 0), List(0, 0, 0))
|
||||
).foreach:
|
||||
case (list, expected) =>
|
||||
test(s"reverseList returns [$expected]"):
|
||||
assertEquals(Problem05.reverseList(list), expected)
|
||||
assertEquals(Problem05Stdlib.reverseList(list), expected)
|
||||
Reference in New Issue
Block a user