Problem 06
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
|
object Problem06:
|
||||||
|
def isPalindromeList(list: List[Any]): Boolean =
|
||||||
|
@tailrec
|
||||||
|
def loop(cursor: List[Any]): Boolean =
|
||||||
|
cursor match
|
||||||
|
case head :: Nil =>
|
||||||
|
true
|
||||||
|
case head :: tail =>
|
||||||
|
tail.reverse match
|
||||||
|
case e :: t if e == head =>
|
||||||
|
loop(t)
|
||||||
|
case e :: t =>
|
||||||
|
false
|
||||||
|
case Nil =>
|
||||||
|
false
|
||||||
|
case Nil =>
|
||||||
|
true
|
||||||
|
|
||||||
|
loop(list)
|
||||||
|
|
||||||
|
object Problem06Stdlib:
|
||||||
|
def isPalindromeList(list: List[Any]): Boolean =
|
||||||
|
list == list.reverse
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
class Problem06Suite extends munit.FunSuite:
|
||||||
|
List(
|
||||||
|
(List(1), true),
|
||||||
|
(List(), true),
|
||||||
|
(List(0, 0, 0), true),
|
||||||
|
(List(5, 4, 3, 4, 5), true),
|
||||||
|
(List(5, 4, 4, 5), true),
|
||||||
|
(List(1, 2, 3, 4, 5), false),
|
||||||
|
).foreach:
|
||||||
|
case (list, expected) =>
|
||||||
|
test(s"isPalindromeList returns [$expected]"):
|
||||||
|
assertEquals(Problem06.isPalindromeList(list), expected)
|
||||||
|
assertEquals(Problem06Stdlib.isPalindromeList(list), expected)
|
||||||
Reference in New Issue
Block a user