Last active
August 29, 2015 14:04
-
-
Save jackliusr/51fcdbf0a5c108dedd4b to your computer and use it in GitHub Desktop.
hackerank The Love-Letter Mystery in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.immutable.StringOps; | |
object Solution { | |
def main(args: Array[String]) { | |
val lines = io.Source.stdin.getLines().toList | |
for(line <- lines.drop(1)) | |
{ | |
val l = line; | |
var (first, tmpSecond) = l.splitAt(l.size /2 ) | |
val second = if(first.length() != tmpSecond.length()) tmpSecond.drop(1).reverse | |
else tmpSecond.reverse | |
var charPairs = first.zip(second) | |
var changes = charPairs.foldLeft(0) ( (a, b) => a + math.abs( b._1.toInt - b._2.toInt)) | |
println(changes) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment