object DerangedAnagrams {

   
  def groupAnagrams(words: Iterable[String]): Map[String, Set[String]] =
    words.foldLeft (Map[String, Set[String]]()) { (map, word) =>
      val sorted = word.sorted
      val entry = map.getOrElse(sorted, Set.empty)
      map + (sorted -> (entry + word))
    }

  
  def isDeranged(ss: (String, String)): Boolean = 
    ss._1 zip ss._2 forall { case (c1, c2) => c1 != c2 }

  
  def pairWords(as: Iterable[String]): Iterable[(String, String)] =
    if (as.size < 2) Seq() else (as.tail map (as.head -> _)) ++ pairWords(as.tail)

  
  def readLines(url: String): Iterable[String] = 
    io.Source.fromURL(url).getLines().toIterable

  val wordsURL = "http: