Skip to content

Instantly share code, notes, and snippets.

@analysisparalysis334
Created January 13, 2015 16:46
Show Gist options
  • Save analysisparalysis334/d4e71da109526552f26a to your computer and use it in GitHub Desktop.
Save analysisparalysis334/d4e71da109526552f26a to your computer and use it in GitHub Desktop.
private def toNum(c: Char): Int = c match {
case 'X' => 10
case ci if c.isDigit => c.toInt
case _ => throw new IllegalStateException("Can't get here")
}
def validate(s: String): Boolean = {
val validChars = s.filter(c => c.isDigit || c == 'X')
if (validChars.length != 10) {
false
} else {
validChars.zipWithIndex.foldLeft(0){case (sum, (c, i)) => sum + (10 - i) * toNum(c)} % 11 == 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment