Anonymous user
Validate International Securities Identification Number: Difference between revisions
Validate International Securities Identification Number (view source)
Revision as of 14:27, 19 March 2017
, 7 years ago→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(Added Algol W) |
(→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details) |
||
Line 756:
=={{header|Kotlin}}==
As the Luhn test method is only a few lines, it's reproduced here for convenience:
<lang scala>// version 1.
object Isin {
Line 762:
fun isValid(s: String): Boolean {
// check format
if (!s.matches(r)) return false
// validate checksum
Line 770:
in '0'..'9' -> sb.append(c)
in 'A'..'Z' -> sb.append((c.toInt() - 55).toString().padStart(2, '0'))
}
return luhn(sb.toString())
}
private fun luhn(s: String): Boolean {
fun sumDigits(n : Int) = n / 10 + n % 10
val t = s.reversed()
val s1 = t.filterIndexed { i,
val s2 = t.filterIndexed { i,
return (s1 + s2) % 10 == 0
}
Line 785:
fun main(args: Array<String>) {
val isins = arrayOf("US0378331005", "US0373831005", "U50378331005", "US03378331005",
"AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040")
for (isin in isins) println("$isin\t -> ${if (Isin.isValid(isin)) "valid" else "not valid"}")
}</lang>
|