daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest May 16th, 2018 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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() );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top