Skip to content

Instantly share code, notes, and snippets.

@LyndonArmitage
Last active January 25, 2016 23:12
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 LyndonArmitage/b180d1e18c31eec4414b to your computer and use it in GitHub Desktop.
Save LyndonArmitage/b180d1e18c31eec4414b to your computer and use it in GitHub Desktop.
anagram scala file
import java.io.File
import scala.collection.mutable
def sortWord(word: String) = new String(word.toCharArray.sorted)
val wordList = scala.io.Source.fromFile(
new File("wordsEn.txt"))
.getLines()
.filter(_.length > 2)
val toWords = mutable.HashMap[String, List[String]]()
wordList.foreach(word => {
val ordered = sortWord(word)
if (toWords.contains(ordered)) {
toWords.put(ordered, toWords.get(ordered).get :+ word)
} else {
toWords.put(ordered, List[String](word))
}
})
//toWords.filter(_._2.size > 1).foreach(println)
toWords.filter(_._2.size > 1)
.get(sortWord("this"))
val required = Set[Char]('a')
val otherChars = Set[Char]('t', 'h', 's', 'e', 'd', 'i')
val allPossibles = otherChars
.subsets()
.map(_.union(required))
.filter(_.size > 2)
.map(set => {
set.mkString.toCharArray.sorted.mkString
})
allPossibles.foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment