Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,12 @@
def lcsLazy[T](u: IndexedSeq[T], v: IndexedSeq[T]): IndexedSeq[T] = {
def su = subsets(u).to(LazyList)
def sv = subsets(v).to(LazyList)
su.intersect(sv).headOption match{
case Some(sub) => sub
case None => IndexedSeq[T]()
}
}
def subsets[T](u: IndexedSeq[T]): Iterator[IndexedSeq[T]] = {
u.indices.reverseIterator.flatMap{n => u.indices.combinations(n + 1).map(_.map(u))}
}

View file

@ -0,0 +1,7 @@
def lcsRec[T]: (IndexedSeq[T], IndexedSeq[T]) => IndexedSeq[T] = {
case (a +: as, b +: bs) if a == b => a +: lcsRec(as, bs)
case (as, bs) if as.isEmpty || bs.isEmpty => IndexedSeq[T]()
case (a +: as, b +: bs) =>
val (s1, s2) = (lcsRec(a +: as, bs), lcsRec(as, b +: bs))
if(s1.length > s2.length) s1 else s2
}

View file

@ -0,0 +1,11 @@
def lcs[T]: (List[T], List[T]) => List[T] = {
case (_, Nil) => Nil
case (Nil, _) => Nil
case (x :: xs, y :: ys) if x == y => x :: lcs(xs, ys)
case (x :: xs, y :: ys) => {
(lcs(x :: xs, ys), lcs(xs, y :: ys)) match {
case (xs, ys) if xs.length > ys.length => xs
case (xs, ys) => ys
}
}
}

View file

@ -0,0 +1,16 @@
case class Memoized[A1, A2, B](f: (A1, A2) => B) extends ((A1, A2) => B) {
val cache = scala.collection.mutable.Map.empty[(A1, A2), B]
def apply(x: A1, y: A2) = cache.getOrElseUpdate((x, y), f(x, y))
}
lazy val lcsM: Memoized[List[Char], List[Char], List[Char]] = Memoized {
case (_, Nil) => Nil
case (Nil, _) => Nil
case (x :: xs, y :: ys) if x == y => x :: lcsM(xs, ys)
case (x :: xs, y :: ys) => {
(lcsM(x :: xs, ys), lcsM(xs, y :: ys)) match {
case (xs, ys) if xs.length > ys.length => xs
case (xs, ys) => ys
}
}
}