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