Task

For each number list of 6-digit SEDOLs, calculate and append the checksum digit.



That is, given this input:

710889
B0YBKJ
406566
B0YBLH
228276
B0YBKL
557910
B0YBKR
585284
B0YBKT
B00030

Produce this output:

7108899
B0YBKJ7
4065663
B0YBLH2
2282765
B0YBKL9
5579107
B0YBKR5
5852842
B0YBKT7
B000300

Extra credit

Check each input is correctly formed, especially with respect to valid characters allowed in a SEDOL string.



Related tasks

   Luhn test
   ISIN


val .csd = f(.code) {
    given len(.code) {
        case 0:
            return "nada, zip, zilch"
        case != 6:
            return "invalid length"
    }
 
    if matching(re/[^B-DF-HJ-NP-TV-Z0-9]/, .code) {
        return "invalid character(s)"
    }
 
    val .weight = [1,3,1,7,3,9]
 
    val .nums = s2n .code
    val .sum = for[=0] .i of .nums {
        _for += .nums[.i] x .weight[.i]
    }
 
    toString 9 - (.sum - 1) rem 10
}
 
val .h = h{
    # invalid...
    "": 0,
    "123": 0,
    "A00030": 0,
    "E00030": 0,
    "I00030": 0,
    "O00030": 0,
    "U00030": 0,
    "β00030": 0,
 
    # valid...
    "710889": 9,
    "B0YBKJ": 7,
    "406566": 3,
    "B0YBLH": 2,
    "228276": 5,
    "B0YBKL": 9,
    "557910": 7,
    "B0YBKR": 5,
    "585284": 2,
    "B0YBKT": 7,
    "B00030": 0,
}
 
for .input in sort(keys .h) {
    val .d = .csd(.input)
    if len(.d) > 1 {
        writeln .input, ": ", .d
    } else {
        val .expect = toString .h[.input]
        write .input, .d
        writeln if .expect == .d {""} else {
            $" (SEDOL test failed; expected check digit \.expect;)"}
    }
}