Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
8
Task/Combinations/Scala/combinations-1.scala
Normal file
8
Task/Combinations/Scala/combinations-1.scala
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
implicit def toComb(m: Int) = new AnyRef {
|
||||
def comb(n: Int) = recurse(m, List.range(0, n))
|
||||
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
|
||||
case (0, _) => List(Nil)
|
||||
case (_, Nil) => Nil
|
||||
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
|
||||
}
|
||||
}
|
||||
8
Task/Combinations/Scala/combinations-2.scala
Normal file
8
Task/Combinations/Scala/combinations-2.scala
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def combs[A](n: Int, l: List[A]): Iterator[List[A]] = n match {
|
||||
case _ if n < 0 || l.lengthCompare(n) < 0 => Iterator.empty
|
||||
case 0 => Iterator(List.empty)
|
||||
case n => l.tails.flatMap({
|
||||
case Nil => Nil
|
||||
case x :: xs => combs(n - 1, xs).map(x :: _)
|
||||
})
|
||||
}
|
||||
16
Task/Combinations/Scala/combinations-3.scala
Normal file
16
Task/Combinations/Scala/combinations-3.scala
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def combs[A](n: Int, xs: List[A]): Stream[List[A]] =
|
||||
combsBySize(xs)(n)
|
||||
|
||||
def combsBySize[A](xs: List[A]): Stream[Stream[List[A]]] = {
|
||||
val z: Stream[Stream[List[A]]] = Stream(Stream(List())) ++ Stream.continually(Stream.empty)
|
||||
xs.toStream.foldRight(z)((a, b) => zipWith[Stream[List[A]]](_ ++ _, f(a, b), b))
|
||||
}
|
||||
|
||||
def zipWith[A](f: (A, A) => A, as: Stream[A], bs: Stream[A]): Stream[A] = (as, bs) match {
|
||||
case (Stream.Empty, _) => Stream.Empty
|
||||
case (_, Stream.Empty) => Stream.Empty
|
||||
case (a #:: as, b #:: bs) => f(a, b) #:: zipWith(f, as, bs)
|
||||
}
|
||||
|
||||
def f[A](x: A, xsss: Stream[Stream[List[A]]]): Stream[Stream[List[A]]] =
|
||||
Stream.empty #:: xsss.map(_.map(x :: _))
|
||||
2
Task/Combinations/Scala/combinations-4.scala
Normal file
2
Task/Combinations/Scala/combinations-4.scala
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
scala>(0 to 4).combinations(3).toList
|
||||
res0: List[scala.collection.immutable.IndexedSeq[Int]] = List(Vector(0, 1, 2), Vector(0, 1, 3), Vector(0, 1, 4), Vector(0, 2, 3), Vector(0, 2, 4), Vector(0, 3, 4), Vector(1, 2, 3), Vector(1, 2, 4), Vector(1, 3, 4), Vector(2, 3, 4))
|
||||
Loading…
Add table
Add a link
Reference in a new issue