State name puzzle: Difference between revisions

m
Line 1,322:
<lang Scala>object StateNamePuzzle extends App {
// Logic:
def disjountPairsdisjointPairs(pairs: Seq[Set[String]]) =
for (a <- pairs; b <- pairs; if a.intersect(b).isEmpty) yield Set(a,b)
 
Line 1,328:
(for (a <- words; b <- words; if a != b) yield Set(a, b)) // all pairs
.groupBy(_.mkString.toLowerCase.replaceAll("[^a-z]", "").sorted) // grouped anagram pairs
.values.map(disjountPairdisjointPairs).flatMap(_.distinct) // unique non-overlapping anagram pairs
 
// Test:
Anonymous user