lundi 28 mars 2016

Scala For Loop, If Statements

CAN YOU RECOMMEND A BETTER WAY TO DO THIS?

val args = "The file on life was now won by the person who saw how to explore."

val argsSeq: Seq[String] = args3.replaceAll("[^a-zA-Z0-9\\s]", "").toLowerCase.split(" ").sorted

for(i <- 0 until argsSeq.length){
  for(j <- 0 until argsSeq.length){
    if((argsSeq(i).toCharArray.sorted.deep == argsSeq(j).toCharArray.sorted.deep)) {
      if((argsSeq(i).toCharArray.deep != argsSeq(j).toCharArray.deep))
      {
        println(argsSeq(i) + " " + argsSeq(j))
      }
    }
  }     
}

file life

how who

life file

now won

saw was

was saw

who how

won now

First, remove the duplicate anagram pairs.

Second, alternative to the for ... for ... if ... if structure?

Thank you.

Aucun commentaire:

Enregistrer un commentaire