Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,25 +1,27 @@
|
|||
import std.stdio, std.algorithm, std.string, std.numeric, std.ascii;
|
||||
|
||||
char sedolChecksum(in char[] sedol) /*pure*/ nothrow
|
||||
char sedolChecksum(in char[] sedol) pure nothrow
|
||||
in {
|
||||
assert(sedol.length == 6, "SEDOL must be 6 chars long.");
|
||||
enum uint mask = 0b11_1110_1111_1011_1110_1110_1110;
|
||||
|
||||
foreach (c; sedol)
|
||||
assert(isDigit(c) ||
|
||||
foreach (immutable c; sedol)
|
||||
assert(c.isDigit ||
|
||||
(c > 'A' && c <= 'Z' && ((1U << (c - 'A')) & mask)),
|
||||
"SEDOL with wrong char.");
|
||||
} out(result) {
|
||||
assert(isDigit(result));
|
||||
static int c2v(in dchar c){ return isDigit(c) ? c-'0' : c-'A'+10; }
|
||||
immutable int d = sedol.map!c2v().dotProduct([1,3,1,7,3,9]);
|
||||
assert(result.isDigit);
|
||||
static int c2v(in dchar c) pure nothrow {
|
||||
return c.isDigit ? c - '0' : c - 'A' + 10;
|
||||
}
|
||||
immutable int d = sedol.map!c2v.dotProduct([1, 3, 1, 7, 3, 9]);
|
||||
assert((d + result - '0') % 10 == 0);
|
||||
} body {
|
||||
enum int[] weights = [1, 3, 1, 7, 3, 9];
|
||||
|
||||
int sum = 0;
|
||||
foreach (i, c; sedol) {
|
||||
if (isDigit(c))
|
||||
foreach (immutable i, immutable c; sedol) {
|
||||
if (c.isDigit)
|
||||
sum += (c - '0') * weights[i];
|
||||
else
|
||||
sum += (c - 'A' + 10) * weights[i];
|
||||
|
|
@ -29,7 +31,8 @@ in {
|
|||
}
|
||||
|
||||
void main() {
|
||||
foreach (s; ["710889", "B0YBKJ", "406566", "B0YBLH", "228276",
|
||||
"B0YBKL", "557910", "B0YBKR", "585284", "B0YBKT"])
|
||||
writeln(s, s.sedolChecksum());
|
||||
foreach (immutable s; ["710889", "B0YBKJ", "406566", "B0YBLH",
|
||||
"228276", "B0YBKL", "557910", "B0YBKR",
|
||||
"585284", "B0YBKT"])
|
||||
writeln(s, s.sedolChecksum);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,41 @@
|
|||
Sedol_char = "0123456789BCDFGHJKLMNPQRSTVWXYZ"
|
||||
|
||||
def char2value(c)
|
||||
raise "No vowels" if 'AEIOU'.include?(c)
|
||||
raise "No vowels" unless Sedol_char.include?(c)
|
||||
c.to_i(36)
|
||||
end
|
||||
|
||||
Sedolweight = [1,3,1,7,3,9]
|
||||
|
||||
def checksum(sedol)
|
||||
tmp = sedol.split('').zip(Sedolweight).map { |ch, weight|
|
||||
char2value(ch) * weight }.inject(0) { |sum, x|
|
||||
sum + x }
|
||||
((10 - (tmp % 10)) % 10).to_s
|
||||
raise "Invalid length" unless sedol.size == Sedolweight.size
|
||||
sum = sedol.split('').zip(Sedolweight).map { |ch, weight|
|
||||
char2value(ch) * weight }.inject(:+)
|
||||
((10 - (sum % 10)) % 10).to_s
|
||||
end
|
||||
|
||||
for sedol in %w{
|
||||
710889
|
||||
B0YBKJ
|
||||
406566
|
||||
B0YBLH
|
||||
228276
|
||||
B0YBKL
|
||||
557910
|
||||
B0YBKR
|
||||
585284
|
||||
B0YBKT
|
||||
}
|
||||
data = %w{
|
||||
710889
|
||||
B0YBKJ
|
||||
406566
|
||||
B0YBLH
|
||||
228276
|
||||
B0YBKL
|
||||
557910
|
||||
B0YBKR
|
||||
585284
|
||||
B0YBKT
|
||||
B00030
|
||||
C0000
|
||||
1234567
|
||||
00000A
|
||||
}
|
||||
|
||||
for sedol in data
|
||||
print "%-8s " % sedol
|
||||
begin
|
||||
puts sedol + checksum(sedol)
|
||||
rescue => e
|
||||
p e
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue