Created
November 25, 2009 10:30
-
-
Save MrJaba/242620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io.Source; | |
import scala.collection.mutable; | |
class Anagrams(file:String){ | |
private val combinations = mutable.Map.empty[String, List[String]]; | |
def run() : List[List[String]] = { | |
for( word <- Source.fromFile(file).getLines ){ | |
val characterStr = sortByChars(word.trim); | |
val appendTo = combinations.getOrElseUpdate(characterStr,List()); | |
combinations(characterStr) = word::appendTo | |
} | |
return combinations.values.toList filter ( _.length > 1 ) | |
} | |
def sortByChars(str:String) : String = { | |
return str.toList.sort( (a,b) => a > b ).mkString; | |
} | |
} | |
val a = new Anagrams( "/usr/share/dict/words" ); | |
println( a.run() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment