RosettaCodeData/Task/SEDOLs/D/sedols-1.d

21 lines
683 B
D
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
import std.stdio, std.algorithm, std.string, std.numeric, std.ascii;
2015-02-20 00:35:01 -05:00
char checksum(in char[] sedol) pure @safe /*@nogc*/
2013-04-10 23:57:08 -07:00
in {
assert(sedol.length == 6);
2015-02-20 00:35:01 -05:00
foreach (immutable c; sedol)
assert(c.isDigit || (c.isUpper && !"AEIOU".canFind(c)));
2013-04-10 23:57:08 -07:00
} out (result) {
2015-02-20 00:35:01 -05:00
assert(result.isDigit);
2013-04-10 23:57:08 -07:00
} body {
2015-02-20 00:35:01 -05:00
static immutable c2v = (in dchar c) => c.isDigit ? c - '0' : (c - 'A' + 10);
immutable int d = sedol.map!c2v.dotProduct([1, 3, 1, 7, 3, 9]);
return digits[10 - (d % 10)];
2013-04-10 23:57:08 -07:00
}
void main() {
2015-02-20 00:35:01 -05:00
foreach (const sedol; "710889 B0YBKJ 406566 B0YBLH 228276
B0YBKL 557910 B0YBKR 585284 B0YBKT".split)
writeln(sedol, sedol.checksum);
2013-04-10 23:57:08 -07:00
}