fork download
  1. import scala.io.Source.{fromFile, stdin}
  2.  
  3. object Main {
  4. def main(args: Array[String]) {
  5. val input = if (args.length >= 1) fromFile(args(0)) else stdin
  6. input.getLines.filterNot(_.isEmpty).map(beauty).foreach(println)
  7. }
  8.  
  9. def isAlpha(c: Char) = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
  10.  
  11. def desc[T: Ordering] = implicitly[Ordering[T]].reverse
  12.  
  13. def beauty(s: String) = s // "abcC!"
  14. .filter(isAlpha) // "abcC"
  15. .toLowerCase // "abcc"
  16. .groupBy(identity) // [a -> a, b -> b, c -> cc]
  17. .mapValues(_.size) // [a -> 1, b -> 1, c -> 2]
  18. .values.toList // [1, 1, 2]
  19. .sortBy(identity)(desc) // [2, 1, 1]
  20. .zip(26 to 1 by -1) // [(2, 26), (1, 25), (1, 24)]
  21. .map(x => x._1 * x._2) // [52, 25, 24]
  22. .sum // 101
  23. }
  24.  
Success #stdin #stdout 0.44s 382208KB
stdin
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves
stdout
152
754
491
729
646