Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
6
Task/SEDOLs/Clojure/sedols.clj
Normal file
6
Task/SEDOLs/Clojure/sedols.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(defn sedols [xs]
|
||||
(letfn [(sedol [ys] (let [weights [1 3 1 7 3 9]
|
||||
convtonum (map #(Character/getNumericValue %) ys)
|
||||
check (-> (reduce + (map * weights convtonum)) (rem 10) (->> (- 10)) (rem 10))]
|
||||
(str ys check)))]
|
||||
(map #(sedol %) xs)))
|
||||
|
|
@ -1,20 +1,20 @@
|
|||
import std.stdio, std.algorithm, std.string, std.numeric, std.ascii;
|
||||
|
||||
char checksum(in char[] sedol)
|
||||
char checksum(in char[] sedol) pure @safe /*@nogc*/
|
||||
in {
|
||||
assert(sedol.length == 6);
|
||||
foreach (c; sedol)
|
||||
assert(isDigit(c) || (isUpper(c) && !canFind("AEIOU", c)));
|
||||
foreach (immutable c; sedol)
|
||||
assert(c.isDigit || (c.isUpper && !"AEIOU".canFind(c)));
|
||||
} out (result) {
|
||||
assert(isDigit(result));
|
||||
assert(result.isDigit);
|
||||
} 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);
|
||||
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)];
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (sedol; "710889 B0YBKJ 406566 B0YBLH 228276
|
||||
B0YBKL 557910 B0YBKR 585284 B0YBKT".split())
|
||||
writeln(sedol, checksum(sedol));
|
||||
foreach (const sedol; "710889 B0YBKJ 406566 B0YBLH 228276
|
||||
B0YBKL 557910 B0YBKR 585284 B0YBKT".split)
|
||||
writeln(sedol, sedol.checksum);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
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 @safe /*@nogc*/
|
||||
in {
|
||||
assert(sedol.length == 6, "SEDOL must be 6 chars long.");
|
||||
enum uint mask = 0b11_1110_1111_1011_1110_1110_1110;
|
||||
|
|
@ -11,13 +11,13 @@ in {
|
|||
"SEDOL with wrong char.");
|
||||
} out(result) {
|
||||
assert(result.isDigit);
|
||||
static int c2v(in dchar c) pure nothrow {
|
||||
static int c2v(in dchar c) pure nothrow @safe @nogc {
|
||||
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];
|
||||
static immutable int[] weights = [1, 3, 1, 7, 3, 9];
|
||||
|
||||
int sum = 0;
|
||||
foreach (immutable i, immutable c; sedol) {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,9 @@
|
|||
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());
|
||||
import std.stdio, std.algorithm, std.string, std.numeric,std.ascii;
|
||||
|
||||
foreach (const s; "710889 B0YBKJ 406566 B0YBLH 228276
|
||||
B0YBKL 557910 B0YBKR 585284 B0YBKT".split)
|
||||
writeln(s, '0' + 10 - s
|
||||
.map!(c => c.isDigit ? c - '0' : c - 'A' + 10)
|
||||
.dotProduct([1, 3, 1, 7, 3, 9]) % 10);
|
||||
}
|
||||
|
|
|
|||
79
Task/SEDOLs/Delphi/sedols.delphi
Normal file
79
Task/SEDOLs/Delphi/sedols.delphi
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
program Sedol;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
|
||||
const
|
||||
SEDOL_CHR_COUNT = 6;
|
||||
DIGITS = ['0'..'9'];
|
||||
LETTERS = ['A'..'Z'];
|
||||
VOWELS = ['A', 'E', 'I', 'O', 'U'];
|
||||
ACCEPTABLE_CHRS = DIGITS + LETTERS - VOWELS;
|
||||
WEIGHTS : ARRAY [1..SEDOL_CHR_COUNT] of integer = (1, 3, 1, 7, 3, 9);
|
||||
LETTER_OFFSET = 9;
|
||||
|
||||
|
||||
function AddSedolCheckDigit(Sedol : string) : string;
|
||||
var
|
||||
iChr : integer;
|
||||
Checksum : integer;
|
||||
CheckDigit : char;
|
||||
begin
|
||||
if Sedol <> uppercase(Sedol) then
|
||||
raise ERangeError.CreateFmt('%s contains lower case characters',[Sedol]);
|
||||
if length(Sedol) <> SEDOL_CHR_COUNT then
|
||||
raise ERangeError.CreateFmt('"%s" length is invalid. Should be 6 characters',[Sedol]);
|
||||
|
||||
Checksum := 0;
|
||||
for iChr := 1 to SEDOL_CHR_COUNT do
|
||||
begin
|
||||
|
||||
if Sedol[iChr] in Vowels then
|
||||
raise ERangeError.CreateFmt('%s contains a vowel (%s) at chr %d',[Sedol, Sedol[iChr], iChr]);
|
||||
if not (Sedol[iChr] in ACCEPTABLE_CHRS) then
|
||||
raise ERangeError.CreateFmt('%s contains an invalid chr (%s) at position %d',[Sedol, Sedol[iChr], iChr]);
|
||||
|
||||
if Sedol[iChr] in DIGITS then
|
||||
Checksum := Checksum + (ord(Sedol[iChr]) - ord('0')) * WEIGHTS[iChr]
|
||||
else
|
||||
Checksum := Checksum + (ord(Sedol[iChr]) - ord('A') + 1 + LETTER_OFFSET) * WEIGHTS[iChr];
|
||||
|
||||
end;
|
||||
|
||||
Checksum := (Checksum mod 10);
|
||||
if Checksum <> 0 then
|
||||
Checksum := 10 - Checksum;
|
||||
CheckDigit := chr(CheckSum + ord('0'));
|
||||
|
||||
Result := Sedol + CheckDigit;
|
||||
end;
|
||||
|
||||
|
||||
procedure Test(First6 : string);
|
||||
begin
|
||||
writeln(First6, ' becomes ', AddSedolCheckDigit(First6));
|
||||
end;
|
||||
|
||||
|
||||
begin
|
||||
try
|
||||
Test('710889');
|
||||
Test('B0YBKJ');
|
||||
Test('406566');
|
||||
Test('B0YBLH');
|
||||
Test('228276');
|
||||
Test('B0YBKL');
|
||||
Test('557910');
|
||||
Test('B0YBKR');
|
||||
Test('585284');
|
||||
Test('B0YBKT');
|
||||
Test('B00030');
|
||||
except
|
||||
on E : Exception do
|
||||
writeln(E.Message);
|
||||
end;
|
||||
readln;
|
||||
end.
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
Sedol_char = "0123456789BCDFGHJKLMNPQRSTVWXYZ"
|
||||
|
||||
def char2value(c)
|
||||
raise "No vowels" unless Sedol_char.include?(c)
|
||||
raise ArgumentError, "No vowels" unless Sedol_char.include?(c)
|
||||
c.to_i(36)
|
||||
end
|
||||
|
||||
Sedolweight = [1,3,1,7,3,9]
|
||||
|
||||
def checksum(sedol)
|
||||
raise "Invalid length" unless sedol.size == Sedolweight.size
|
||||
raise ArgumentError, "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
|
||||
|
|
|
|||
63
Task/SEDOLs/Transact-SQL/sedols.sql
Normal file
63
Task/SEDOLs/Transact-SQL/sedols.sql
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
CREATE FUNCTION [dbo].[fn_CheckSEDOL]
|
||||
( @SEDOL varchar(50) )
|
||||
RETURNS varchar(7)
|
||||
AS
|
||||
BEGIN
|
||||
declare @true bit = 1,
|
||||
@false bit = 0,
|
||||
@isSEDOL bit,
|
||||
@sedol_weights varchar(6) ='131739',
|
||||
@sedol_len int = LEN(@SEDOL),
|
||||
@sum int = 0
|
||||
|
||||
|
||||
if ((@sedol_len = 6))
|
||||
begin
|
||||
select @SEDOL = UPPER(@SEDOL)
|
||||
Declare @vowels varchar(5) = 'AEIOU',
|
||||
@letters varchar(21) = 'BCDFGHJKLMNPQRSTVWXYZ',
|
||||
@i int=1,
|
||||
@isStillGood bit = @true,
|
||||
@char char = '',
|
||||
@weighting int =0
|
||||
|
||||
select @isSEDOL = @false
|
||||
|
||||
while ((@i < 7) and (@isStillGood = @true))
|
||||
begin
|
||||
select @char = SUBSTRING(@SEDOL,@i,1),
|
||||
@weighting = CONVERT (INT,SUBSTRING(@sedol_weights, @i, 1))
|
||||
if (CHARINDEX(@char, @vowels) > 0) -- no vowels please
|
||||
begin
|
||||
select @isStillGood=@false
|
||||
end
|
||||
else
|
||||
begin
|
||||
if (ISNUMERIC(@char) = @true) -- is a number
|
||||
begin
|
||||
select @sum = @sum + (ASCII(@char) - 48) * @weighting
|
||||
end
|
||||
else if (CHARINDEX(@char, @letters) = 0) -- test for the rest of the alphabet
|
||||
begin
|
||||
select @isStillGood=@false
|
||||
end
|
||||
else
|
||||
begin
|
||||
select @sum = @sum + (ASCII(@char) - 55) * @weighting
|
||||
end
|
||||
end
|
||||
select @i = @i +1
|
||||
end -- of while loop
|
||||
if (@isStillGood = @true)
|
||||
begin
|
||||
declare @checksum int = (10 - (@sum%10))%10
|
||||
select @SEDOL = @SEDOL + CONVERT(CHAR,@checksum)
|
||||
end
|
||||
end
|
||||
else
|
||||
begin
|
||||
select @SEDOL = ''
|
||||
end
|
||||
-- Return the result of the function
|
||||
RETURN @SEDOL
|
||||
END
|
||||
Loading…
Add table
Add a link
Reference in a new issue