Problem 31
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
package ninetyNineScalaProblems.lists
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Problem28:
|
||||
def lsort(list: List[List[Any]]): List[Any] =
|
||||
list.sortWith((a, b) => a.length < b.length)
|
||||
@@ -0,0 +1,21 @@
|
||||
package ninetyNineScalaProblems.arithmetic
|
||||
|
||||
import scala.annotation.tailrec
|
||||
import scala.math.sqrt
|
||||
|
||||
object Problem31:
|
||||
def isPrime(int: Int): Boolean =
|
||||
@tailrec
|
||||
def loop(cursor: Int): Boolean =
|
||||
cursor match
|
||||
case c if c >= sqrt(int).toInt + 1 =>
|
||||
true
|
||||
case c if int % c == 0 =>
|
||||
false
|
||||
case _ =>
|
||||
loop(cursor + 1)
|
||||
int match
|
||||
case int if int < 2 =>
|
||||
false
|
||||
case _ =>
|
||||
loop(2)
|
||||
@@ -1,15 +0,0 @@
|
||||
package ninetyNineScalaProblems.lists
|
||||
|
||||
class Problem28Suite extends munit.FunSuite:
|
||||
List(
|
||||
(List(List(1, 2), List(1, 2, 3, 4), List(1, 2, 3)), List(List(1, 2), List(1, 2, 3), List(1, 2, 3, 4))),
|
||||
(List(List(1, 2), List(1, 2, 3), List(1, 2, 3, 4)), List(List(1, 2), List(1, 2, 3), List(1, 2, 3, 4))),
|
||||
(List(), List()),
|
||||
(List(List(1)), List(List(1))),
|
||||
(List(List(1), List(1)), List(List(1), List(1))),
|
||||
(List(List(1, 2, 3), List(4, 5), List(6, 7, 8), List(4, 5), List(9, 10, 11, 12), List(13, 14), List(15)),
|
||||
List(List(15), List(4, 5), List(4, 5), List(13, 14), List(1, 2, 3), List(6, 7, 8), List(9, 10, 11, 12))),
|
||||
).foreach:
|
||||
case (list, expected) =>
|
||||
test(s"lsort returns [$expected]"):
|
||||
assertEquals(Problem28.lsort(list), expected)
|
||||
@@ -0,0 +1,16 @@
|
||||
package ninetyNineScalaProblems.arithmetic
|
||||
|
||||
class Problem31Suite extends munit.FunSuite:
|
||||
List(
|
||||
(1, false),
|
||||
(0, false),
|
||||
(-1, false),
|
||||
(2, true),
|
||||
(3, true),
|
||||
(4, false),
|
||||
(5, true),
|
||||
(71, true),
|
||||
).foreach:
|
||||
case (int, expected) =>
|
||||
test(s"isPrime returns [$expected]"):
|
||||
assertEquals(Problem31.isPrime(int), expected)
|
||||
Reference in New Issue
Block a user