Problem 18
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Problem18:
|
||||
def slice(i: Int, k: Int, list: List[Any]): List[Any] =
|
||||
@tailrec
|
||||
def loop(cursor: List[Any], acc: List[Any], index: Int): List[Any] =
|
||||
cursor match
|
||||
case e :: tail if (i <= index && index < k) =>
|
||||
loop(tail, e :: acc, index + 1)
|
||||
case e :: tail =>
|
||||
loop(tail, acc, index + 1)
|
||||
case Nil =>
|
||||
acc
|
||||
|
||||
loop(list, List(), 0).reverse
|
||||
@@ -0,0 +1,18 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.compiletime.ops.boolean
|
||||
|
||||
class Problem18Suite extends munit.FunSuite:
|
||||
List(
|
||||
((0, 0, List(1, 2, 3, 4)), List()),
|
||||
((0, 1, List(1, 2, 3, 4)), List(1)),
|
||||
((1, 1, List(1, 2, 3, 4)), List()),
|
||||
((1, 2, List(1, 2, 3, 4)), List(2)),
|
||||
((1, 3, List(1, 2, 3, 4)), List(2, 3)),
|
||||
((1, 4, List(1, 2, 3, 4)), List(2, 3, 4)),
|
||||
((1, 5, List(1, 2, 3, 4)), List(2, 3, 4)),
|
||||
((1, 0, List(1, 2, 3, 4)), List()),
|
||||
).foreach:
|
||||
case ((i, k, list), expected) =>
|
||||
test(s"slice returns [$expected]"):
|
||||
assertEquals(Problem18.slice(i, k, list), expected)
|
||||
Reference in New Issue
Block a user