Skip to content

Instantly share code, notes, and snippets.

@MrJaba
Created November 25, 2009 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrJaba/242620 to your computer and use it in GitHub Desktop.
Save MrJaba/242620 to your computer and use it in GitHub Desktop.
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