Created
April 14, 2017 11:59
-
-
Save onegrx/596f3a182aa9111821afd67d2dd3fb6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Primes extends App { | |
def process(A: List[Int], B: List[Int]) = { | |
def isPrime(n: Int): Boolean = { | |
if (n == 2) true | |
else if (n < 2 || n % 2 == 0) false | |
else Stream.from(3, 2).takeWhile(i => i * i <= n).forall(n % _ != 0) | |
} | |
val map = scala.collection.mutable.Map[Int, Int]() | |
B.foreach(i => { | |
val count = if (map contains i) map(i) else 0 | |
map.put(i, count + 1) | |
}) | |
A.filter(i => !B.contains(i) || !isPrime(map(i))) | |
} | |
//example | |
val A = List(2, 3, 9, 2, 5, 1, 3, 7, 10) | |
val B = List(2, 1, 3, 4, 3, 10, 6, 6, 1, 7, 10, 10, 10) | |
process(A, B) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment