September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,43 @@
begin
% returns the check digit for the specified SEDOL %
string(1) procedure sedolCheckDigit ( string(6) value sedol ) ;
begin
integer checkSum, checkDigit;
checkSum := 0;
for cPos := 0 until 5 do begin
string(1) c;
integer digit;
c := sedol( cPos // 1 );
if c >= "0" and c <= "9"
then digit := decode( c ) - decode( "0" )
else digit := 10 + ( decode( c ) - decode( "A" ) );
checkSum := checkSum + ( ( case cPos + 1 of ( 1, 3, 1, 7, 3, 9 ) ) * digit )
end for_cPos ;
checkDigit := ( 10 - ( checkSum rem 10 ) ) rem 10;
if checkDigit < 10
then code( decode( "0" ) + checkDigit )
else code( decode( "A" ) + ( checkDigit - 10 ) )
end sedolCheckDigit ;
% task test cases %
procedure testCheckDigit ( string(6) value sedol; string(1) value expectedCheckDigit ) ;
begin
string(1) checkDigit;
checkDigit := sedolCheckDigit( sedol );
write( s_w := 0, sedol, checkDigit );
if checkDigit not = expectedCheckDigit then writeon( " ?? expected: ", expectedCheckDigit )
end testCheckDigit ;
testCheckDigit( "710889", "9" );
testCheckDigit( "B0YBKJ", "7" );
testCheckDigit( "406566", "3" );
testCheckDigit( "B0YBLH", "2" );
testCheckDigit( "228276", "5" );
testCheckDigit( "B0YBKL", "9" );
testCheckDigit( "557910", "7" );
testCheckDigit( "B0YBKR", "5" );
testCheckDigit( "585284", "2" );
testCheckDigit( "B0YBKT", "7" );
testCheckDigit( "B00030", "0" )
end.

View file

@ -0,0 +1,19 @@
Class Utils.Check [ Abstract ]
{
ClassMethod SEDOL(x As %String) As %Boolean
{
// https://en.wikipedia.org/wiki/SEDOL
IF x'?1(7N,1U5UN1N) QUIT 0
IF x'=$TRANSLATE(x,"AEIOU") QUIT 0
SET cd=$EXTRACT(x,*), x=$EXTRACT(x,1,*-1)
SET wgt="1317391", t=0
FOR i=1:1:$LENGTH(x) {
SET n=$EXTRACT(x,i)
IF n'=+n SET n=$ASCII(n)-55
SET t=t+(n*$EXTRACT(wgt,i))
}
QUIT cd=((10-(t#10))#10)
}
}

View file

@ -1,22 +1,31 @@
import Data.Char
import Data.Char (isDigit, isAsciiUpper, ord)
char2value c | c `elem` "AEIOU" = error "No vowels."
| c >= '0' && c <= '9' = ord c - ord '0'
| c >= 'A' && c <= 'Z' = ord c - ord 'A' + 10
checkSum :: String -> String
checkSum =
show .
(`rem` 10) .
(-) 10 . (`rem` 10) . sum . zipWith (*) [1, 3, 1, 7, 3, 9] . (charValue <$>)
sedolweight = [1,3,1,7,3,9]
charValue :: Char -> Int
charValue c
| c `elem` "AEIOU" = error "No vowels."
| isDigit c = ord c - ord '0'
| isAsciiUpper c = ord c - ord 'A' + 10
checksum sedol = show ((10 - (tmp `mod` 10)) `mod` 10)
where tmp = sum $ zipWith (*) sedolweight $ map char2value sedol
main = mapM_ (\sedol -> putStrLn $ sedol ++ checksum sedol)
[ "710889",
"B0YBKJ",
"406566",
"B0YBLH",
"228276",
"B0YBKL",
"557910",
"B0YBKR",
"585284",
"B0YBKT" ]
-- TEST ----------------------------------------------------------------------
main :: IO ()
main =
mapM_
(putStrLn . ((++) <*> checkSum))
[ "710889"
, "B0YBKJ"
, "406566"
, "B0YBLH"
, "228276"
, "B0YBKL"
, "557910"
, "B0YBKR"
, "585284"
, "B0YBKT"
, "B00030"
]

View file

@ -0,0 +1,25 @@
// version 1.1.0
val weights = listOf(1, 3, 1, 7, 3, 9, 1)
fun sedol7(sedol6: String): String {
if (sedol6.length != 6) throw IllegalArgumentException("Length of argument string must be 6")
var sum = 0
for (i in 0..5) {
val c = sedol6[i]
val v = when (c) {
in '0'..'9' -> c.toInt() - 48
in 'A'..'Z' -> c.toInt() - 55
else -> throw IllegalArgumentException("Argument string contains an invalid character")
}
sum += v * weights[i]
}
val check = (10 - (sum % 10)) % 10
return sedol6 + (check + 48).toChar()
}
fun main(args: Array<String>) {
val sedol6s = listOf("710889", "B0YBKJ", "406566", "B0YBLH", "228276", "B0YBKL",
"557910", "B0YBKR", "585284", "B0YBKT", "B00030")
for (sedol6 in sedol6s) println("$sedol6 -> ${sedol7(sedol6)}")
}

View file

@ -0,0 +1,9 @@
fcn checksum(text){
( text.len()!=6 or (text..matches("*[AEIOUaeioua-z]*")) ) and
throw(Exception.ValueError("Invalid SEDOL text: "+text));
text + (10 - text.pump(List,'wrap(c){
if("0"<=c<="9") c.toAsc()-0x30;
else c.toAsc()-55;
}).zipWith('*,T(1,3,1,7,3,9)).sum() % 10) % 10;
}

View file

@ -0,0 +1,3 @@
T("710889","B0YBKJ","406566","B0YBLH","228276",
"B0YBKL","557910","B0YBKR","585284","B0YBKT","B00030")
.apply(checksum).println();