Skip to content

Instantly share code, notes, and snippets.

@tejom
Created July 4, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejom/73771cf453ef5a36e7325c5911286fe4 to your computer and use it in GitHub Desktop.
Save tejom/73771cf453ef5a36e7325c5911286fe4 to your computer and use it in GitHub Desktop.
import cats.data.State
import cats.instances.vector._
import scala.language.higherKinds
import cats.syntax.applicative._
object julythree {
def main(args: Array[String]): Unit = {
val ins = List(List('0', '1'), List('A', 'B', 'C'), List('D', 'E', 'F', 'G'))
val ins2 = List(List('0', '1', '2', '3'), List('A', 'B', 'C', 'D'), List('E', 'F', 'G', 'H', 'I'))
val ins3= List(List('0', '1', '2', '3', '4'),
List('A', 'B', 'C', 'D', 'E'),
List('F', 'G', 'H', 'I'),
List('J', 'K', 'L'))
val list_pairs = ins3.foldLeft(List(List[Any]()))( (acc,next) =>
if (acc.isEmpty) {
next.map(List(_))
} else {
acc.flatMap( e => next.map( x => {x :: e})
)}).map(_.reverse)
type pairState[A] = State[Set[Any],A]
def check(curr: List[Any],l:List[Any]): pairState[List[Any]] =
State[Set[Any],List[Any]]{ seen => {
val pairs = l.sliding(2).toSet
val c = pairs.map( (e) => seen.contains(e)).reduce(_&&_)
val f = if(c) List() else l
(pairs ++ seen,curr ++ List(f))
}}
val r = scala.util.Random.shuffle(list_pairs).foldLeft( List[Any]().pure[pairState])( (m,next) => {
m.flatMap( x => check(x,next) )
})
val filtered_list = r.run(Set()).value._2.filter( _ match {
case _ :: _ => true
case _ => false
}
)
filtered_list.foreach(a => println(a))
println(filtered_list.length)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment