Problem 25
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
import scala.util.Random
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
|
object Problem25:
|
||||||
|
def randomPermute(list: List[Any]): List[Any] =
|
||||||
|
@tailrec
|
||||||
|
def loop(cursor: List[Any], acc: List[Any]): List[Any] =
|
||||||
|
cursor match
|
||||||
|
case head :: tail =>
|
||||||
|
loop(tail, cursor(Random.between(0, cursor.length)) :: acc)
|
||||||
|
case Nil =>
|
||||||
|
acc
|
||||||
|
|
||||||
|
loop(list, List())
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
class Problem25Suite extends munit.FunSuite:
|
||||||
|
List(
|
||||||
|
(List(1), List(1)),
|
||||||
|
(List(), List()),
|
||||||
|
).foreach:
|
||||||
|
case (list, expected) =>
|
||||||
|
test(s"randomPermute returns [$expected]"):
|
||||||
|
assertEquals(Problem25.randomPermute(list), expected)
|
||||||
Reference in New Issue
Block a user