Guest User

Untitled

a guest
May 16th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. import scala.io.Source;
  2. import scala.collection.mutable;
  3.  
  4. class Anagrams(file:String){
  5.  
  6. private val combinations = mutable.Map.empty[String, List[String]];
  7.  
  8. def run() : List[List[String]] = {
  9. for( word <- Source.fromFile(file).getLines ){
  10. val characterStr = sortByChars(word.trim);
  11. val appendTo = combinations.getOrElseUpdate(characterStr,List());
  12. combinations(characterStr) = word::appendTo
  13. }
  14. return combinations.values.toList filter ( _.length > 1 )
  15. }
  16.  
  17. def sortByChars(str:String) : String = {
  18. return str.toList.sort( (a,b) => a > b ).mkString;
  19. }
  20.  
  21. }
  22.  
  23. val a = new Anagrams( "/usr/share/dict/words" );
  24. println( a.run() );
Add Comment
Please, Sign In to add comment