Problem 18

This commit is contained in:
2026-03-19 15:39:35 +01:00
parent 233a9369c8
commit 4cb7b4e95b
2 changed files with 35 additions and 0 deletions
@@ -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