Skip to content

Instantly share code, notes, and snippets.

@p3t0r
Created October 9, 2010 15:43
Show Gist options
  • Save p3t0r/618308 to your computer and use it in GitHub Desktop.
Save p3t0r/618308 to your computer and use it in GitHub Desktop.
scrable scorer in scala
#!/usr/bin/env scala
!#
import io.Source
if(args.length < 1) { println("usage: ./scrabble <filename>"); System.exit(-1)}
Source.fromFile(args(0)).getLines.foreach(w => printf("%s,%d%n", w,score(w)))
def score(word:String) = word.foldLeft(0) (_ + scoreChar(_))
def scoreChar(letter:Char) = {
letter match {
case 'a' => 1
case 'b' => 3
case 'c' => 5
case 'd' => 2
case 'e' => 1
case 'f' => 4
case 'g' => 3
case 'h' => 4
case 'i' => 1
case 'j' => 4
case 'k' => 3
case 'l' => 3
case 'm' => 3
case 'n' => 1
case 'o' => 1
case 'p' => 3
case 'q' => 10
case 'r' => 2
case 's' => 2
case 't' => 2
case 'u' => 4
case 'v' => 4
case 'w' => 5
case 'x' => 8
case 'y' => 8
case 'z' => 4
case _ => 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment