Skip to content

Instantly share code, notes, and snippets.

@pfcoperez
Created August 14, 2017 13:10
Show Gist options
  • Save pfcoperez/348efdfbef518274ad11d84f6b79ec7b to your computer and use it in GitHub Desktop.
Save pfcoperez/348efdfbef518274ad11d84f6b79ec7b to your computer and use it in GitHub Desktop.
@tailrec
def permutations[T](alphabet: Set[T], s: Seq[Seq[T]] = Seq(Seq.empty[T])): Seq[Seq[T]] =
if(s.head.size == alphabet.size) s else {
val updatedSolutions = for {
partial <- s
chosen <- (alphabet -- partial)
} yield chosen +: partial
permutations(alphabet, updatedSolutions)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment