import scalaz._ import Scalaz._ object Combi1 { def combine[A](xs: List[A]): List[List[A]] = xs.replicate[List](xs.size).sequence def main(args: Array[String]): Unit = { val list = List(1, 1, 2, 3, 3, 3) def isCorrect(list: List[Int]) : Boolean = { list.count(i => i == 1) == 2 && list.count(i => i == 2) == 1 && list.count(i => i == 3) == 3 } val combi = combine(list).filter(l => isCorrect(l)).distinct println(combi.size + " elements") combi.foreach(l => println(l)) } }
File generation with SBT
10 years ago
1 comment:
That is a much shorter solution. The main trick I was looking for was to only consider valid solutions instead of filtering all possible combinations e.g. if the possible combinations is much, much higher than the number of valid ones.
I suspect this too is simpler in Scala, but I wouldn't know where to start. ;)
Post a Comment