Created
January 13, 2015 16:46
-
-
Save analysisparalysis334/d4e71da109526552f26a to your computer and use it in GitHub Desktop.
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
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