Skip to content

Instantly share code, notes, and snippets.

@lsankar4033
Created January 13, 2015 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsankar4033/12b79b4de24acba17a78 to your computer and use it in GitHub Desktop.
Save lsankar4033/12b79b4de24acba17a78 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