tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

20
Task/SEDOLs/D/sedols-1.d Normal file
View file

@ -0,0 +1,20 @@
import std.stdio, std.algorithm, std.string, std.numeric, std.ascii;
char checksum(in char[] sedol)
in {
assert(sedol.length == 6);
foreach (c; sedol)
assert(isDigit(c) || (isUpper(c) && !canFind("AEIOU", c)));
} out (result) {
assert(isDigit(result));
} body {
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]);
return '0' + 10 - (d % 10);
}
void main() {
foreach (sedol; "710889 B0YBKJ 406566 B0YBLH 228276
B0YBKL 557910 B0YBKR 585284 B0YBKT".split())
writeln(sedol, checksum(sedol));
}

35
Task/SEDOLs/D/sedols-2.d Normal file
View file

@ -0,0 +1,35 @@
import std.stdio, std.algorithm, std.string, std.numeric, std.ascii;
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) ||
(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((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))
sum += (c - '0') * weights[i];
else
sum += (c - 'A' + 10) * weights[i];
}
return '0' + 10 - (sum % 10);
}
void main() {
foreach (s; ["710889", "B0YBKJ", "406566", "B0YBLH", "228276",
"B0YBKL", "557910", "B0YBKR", "585284", "B0YBKT"])
writeln(s, s.sedolChecksum());
}

12
Task/SEDOLs/D/sedols-3.d Normal file
View file

@ -0,0 +1,12 @@
import std.stdio, std.algorithm, std.string, std.numeric;
auto checksum(string s) {
auto m = s.map!q{ a>='0' && a<='9' ? a-'0' : a-'A'+10 }();
return '0' + 10 - m.dotProduct([1,3,1,7,3,9]) % 10;
}
void main() {
foreach (sedol; "710889 B0YBKJ 406566 B0YBLH 228276
B0YBKL 557910 B0YBKR 585284 B0YBKT".split())
writeln(sedol, sedol.checksum());
}