Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Power-set/Scala/power-set-1.scala
Normal file
9
Task/Power-set/Scala/power-set-1.scala
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import scala.compat.Platform.currentTime
|
||||
|
||||
object Powerset extends App {
|
||||
def powerset[A](s: Set[A]) = s.foldLeft(Set(Set.empty[A])) { case (ss, el) => ss ++ ss.map(_ + el)}
|
||||
|
||||
assert(powerset(Set(1, 2, 3, 4)) == Set(Set.empty, Set(1), Set(2), Set(3), Set(4), Set(1, 2), Set(1, 3), Set(1, 4),
|
||||
Set(2, 3), Set(2, 4), Set(3, 4), Set(1, 2, 3), Set(1, 3, 4), Set(1, 2, 4), Set(2, 3, 4), Set(1, 2, 3, 4)))
|
||||
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
|
||||
}
|
||||
1
Task/Power-set/Scala/power-set-2.scala
Normal file
1
Task/Power-set/Scala/power-set-2.scala
Normal file
|
|
@ -0,0 +1 @@
|
|||
def powerset[A](s: Set[A]) = (0 to s.size).map(s.toSeq.combinations(_)).reduce(_ ++ _).map(_.toSet)
|
||||
7
Task/Power-set/Scala/power-set-3.scala
Normal file
7
Task/Power-set/Scala/power-set-3.scala
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def powerset[A](s: Set[A]) = {
|
||||
def powerset_rec(acc: List[Set[A]], remaining: List[A]): List[Set[A]] = remaining match {
|
||||
case Nil => acc
|
||||
case head :: tail => powerset_rec(acc ++ acc.map(_ + head), tail)
|
||||
}
|
||||
powerset_rec(List(Set.empty[A]), s.toList)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue