Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
93
Task/SEDOLs/FreeBASIC/sedols.freebasic
Normal file
93
Task/SEDOLs/FreeBASIC/sedols.freebasic
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
' version 05-07-2015
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function check_sedol(input_nr As String) As Integer
|
||||
input_nr = Trim(input_nr)
|
||||
Dim As Integer i, j, x, nr_begin, sum
|
||||
Dim As String ch, legal = "AEIOU0123456789BCDFGHJKLMNPQRSTVWXYZ"
|
||||
Dim As Integer weight(0 To ...) = { 1, 3, 1, 7, 3, 9, 1}
|
||||
|
||||
x = Len(input_nr)
|
||||
If x < 6 Or x > 7 Then
|
||||
Return -99 ' to long or to short
|
||||
End If
|
||||
|
||||
For i = 0 To 5
|
||||
ch = Chr(input_nr[i])
|
||||
j = InStr(legal,ch)
|
||||
If j < 6 Then
|
||||
Return -90+j ' not a legal char. or a vowel
|
||||
End If
|
||||
j = ch[0] - Asc("0")
|
||||
If j > 9 Then j = j + (Asc("0") + 10- Asc("A"))
|
||||
If i = 0 AndAlso j < 10 Then nr_begin = 1
|
||||
If nr_begin = 1 AndAlso i > 0 Then
|
||||
If j > 9 Then Return -97 ' first is number then all be numbers
|
||||
End If
|
||||
sum = sum + j * weight(i)
|
||||
Next
|
||||
sum= ((10 - (sum Mod 10)) Mod 10)
|
||||
If x = 7 Then
|
||||
j=input_nr[6] - Asc("0") ' checksum digit is only number
|
||||
If j = sum Then
|
||||
Return 100+sum ' correct
|
||||
Else
|
||||
Return -98 ' wrong
|
||||
End If
|
||||
End If
|
||||
|
||||
Return sum ' checksum digit
|
||||
|
||||
End Function
|
||||
|
||||
Sub sedol(in As String)
|
||||
|
||||
Dim As Integer checksum = check_sedol(in)
|
||||
Print(in);
|
||||
|
||||
Select Case checksum
|
||||
Case -99
|
||||
Print " Illegal SEDOL: wrong length"
|
||||
Case -98
|
||||
Print " Illegal SEDOL: checksum digits do not match"
|
||||
Case -97
|
||||
Print " Illegal SEDOL: starts with number, may only contain numbers"
|
||||
Case -90
|
||||
Print " Illegal SEDOL: illegal character"
|
||||
Case -89 To -85
|
||||
Print " Illegal SEDOL: No vowels allowed"
|
||||
Case Is > 99
|
||||
Print " Valid SEDOL: checksums match"
|
||||
Case Else
|
||||
Print " checksum calculated : ";in;Str(checksum)
|
||||
End Select
|
||||
|
||||
End Sub
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As Integer k,checksum
|
||||
Dim As String in(1 To ...) = {"710889", "B0YBKJ", "406566", "B0YBLH",_
|
||||
"228276", "B0YBKL", "557910", "B0YBKR",_
|
||||
"585284", "B0YBKT", "B00030"}
|
||||
|
||||
Print "Calculated checksum"
|
||||
For k = 1 To UBound(in) : sedol(in(k)) : Next
|
||||
|
||||
Print : Print "Check checksum"
|
||||
Dim As String in1(1 To ...) = {"7108899", "B0YBKJ7", "4065663", "B0YBLH2",_
|
||||
"2282765", "B0YBKL9","5579107", "B0YBKR5",_
|
||||
"5852842", "B0YBKT7", "B000300"}
|
||||
|
||||
For k = 1 To UBound(in1) : sedol(in1(k)) : Next
|
||||
|
||||
Print : Print "Error test"
|
||||
Dim As String errors(1 To ...) = {"12", "1234567890", "1B0000", "123 45",_
|
||||
"A00000", "B000301"}
|
||||
|
||||
For k = 1 To UBound(errors) : sedol(errors(k)) : Next
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
28
Task/SEDOLs/Nim/sedols.nim
Normal file
28
Task/SEDOLs/Nim/sedols.nim
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import strutils
|
||||
|
||||
proc c2v(c): int =
|
||||
assert c notin "AEIOU"
|
||||
let a = ord(c)
|
||||
if a < 65: a - 48
|
||||
else: a - 55
|
||||
|
||||
const weight = [1,3,1,7,3,9]
|
||||
|
||||
proc checksum(sedol): string =
|
||||
var tmp = 0
|
||||
for i,s in sedol:
|
||||
tmp += c2v(s) * weight[i]
|
||||
result = $((10 - (tmp mod 10)) mod 10)
|
||||
|
||||
for sedol in """710889
|
||||
B0YBKJ
|
||||
406566
|
||||
B0YBLH
|
||||
228276
|
||||
B0YBKL
|
||||
557910
|
||||
B0YBKR
|
||||
585284
|
||||
B0YBKT
|
||||
B00030""".splitLines():
|
||||
echo sedol, checksum(sedol)
|
||||
5
Task/SEDOLs/Oforth/sedols.oforth
Normal file
5
Task/SEDOLs/Oforth/sedols.oforth
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
func: sedol(s)
|
||||
[ 1, 3, 1, 7, 3, 9 ] s
|
||||
zipWith(#[ dup isDigit ifTrue: [ '0' - ] else: [ 'A' - 10 + ] * ]) sum
|
||||
10 mod 10 swap - 10 mod
|
||||
StringBuffer new s << swap '0' + <<c ;
|
||||
32
Task/SEDOLs/Phix/sedols.phix
Normal file
32
Task/SEDOLs/Phix/sedols.phix
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
type string6(object s)
|
||||
return string(s) and length(s)=6
|
||||
end type
|
||||
|
||||
type sedolch(integer ch)
|
||||
return ch>='0' and ch<='Z' and (ch<='9' or ch>='A') and not find(ch,"AEIOU")
|
||||
end type
|
||||
|
||||
function sedol(string6 t)
|
||||
sedolch c
|
||||
integer s = 0
|
||||
for i=1 to 6 do
|
||||
c = t[i]
|
||||
s += iff(c>='A'?c-'A'+10:c-'0')*{1,3,1,7,3,9}[i]
|
||||
end for
|
||||
return t & mod(10-mod(s,10),10)+'0'
|
||||
end function
|
||||
|
||||
constant tests = {"710889",
|
||||
"B0YBKJ",
|
||||
"406566",
|
||||
"B0YBLH",
|
||||
"228276",
|
||||
"B0YBKL",
|
||||
"557910",
|
||||
"B0YBKR",
|
||||
"585284",
|
||||
"B0YBKT",
|
||||
"B00030"}
|
||||
for i=1 to length(tests) do
|
||||
?sedol(tests[i])
|
||||
end for
|
||||
13
Task/SEDOLs/Potion/sedols.potion
Normal file
13
Task/SEDOLs/Potion/sedols.potion
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
sedolnum = (c) :
|
||||
if ("0" ord <= c ord and c ord <= "9" ord): c number integer.
|
||||
else: 10 + c ord - "A" ord.
|
||||
.
|
||||
|
||||
sedol = (str) :
|
||||
weight = (1, 3, 1, 7, 3, 9)
|
||||
sum = 0
|
||||
6 times (i) :
|
||||
sum = sum + sedolnum(str(i)) * weight(i)
|
||||
.
|
||||
(str, (10 - (sum % 10)) % 10) join
|
||||
.
|
||||
24
Task/SEDOLs/Ring/sedols.ring
Normal file
24
Task/SEDOLs/Ring/sedols.ring
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
see sedol("710889") + nl
|
||||
see sedol("B0YBKJ") + nl
|
||||
see sedol("406566") + nl
|
||||
see sedol("B0YBLH") + nl
|
||||
see sedol("228276") + nl
|
||||
see sedol("B0YBKL") + nl
|
||||
see sedol("557910") + nl
|
||||
see sedol("B0YBKR") + nl
|
||||
see sedol("585284") + nl
|
||||
see sedol("B0YBKT") + nl
|
||||
see sedol("B00030") + nl
|
||||
|
||||
func sedol d
|
||||
d = upper(d)
|
||||
s = 0
|
||||
weights = [1, 3, 1, 7, 3, 9]
|
||||
for i = 1 to 6
|
||||
a = substr(d,i,1)
|
||||
if ascii(a) >= 48 and ascii(a) <= 57
|
||||
s += number(a) * weights[i]
|
||||
else
|
||||
s += (ascii(a) - 55) * weights[i] ok
|
||||
next
|
||||
return d + (10 - (s % 10)) % 10
|
||||
29
Task/SEDOLs/Sidef/sedols.sidef
Normal file
29
Task/SEDOLs/Sidef/sedols.sidef
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
func sedol(s) {
|
||||
|
||||
die 'No vowels allowed' if (s ~~ /[AEIOU]/);
|
||||
die 'Invalid format' if (s !~ /^[0-9B-DF-HJ-NP-TV-Z]{6}$/);
|
||||
|
||||
const base36 = ((@(0..9) + @('A'..'Z')) ~Z @(0..35) -> flatten.to_h);
|
||||
const weights = [1, 3, 1, 7, 3, 9];
|
||||
|
||||
var vs = [base36{ s.chars... }];
|
||||
var checksum = (vs ~Z* weights -> sum);
|
||||
var check_digit = ((10 - checksum%10) % 10);
|
||||
return (s + check_digit);
|
||||
}
|
||||
|
||||
%w(
|
||||
710889
|
||||
B0YBKJ
|
||||
406566
|
||||
B0YBLH
|
||||
228276
|
||||
B0YBKL
|
||||
557910
|
||||
B0YBKR
|
||||
585284
|
||||
B0YBKT
|
||||
B00030
|
||||
).each { |s|
|
||||
say sedol(s);
|
||||
}
|
||||
73
Task/SEDOLs/Visual-FoxPro/sedols.visual
Normal file
73
Task/SEDOLs/Visual-FoxPro/sedols.visual
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#DEFINE ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
#DEFINE VOWELS "AEIOU"
|
||||
#DEFINE VALIDCHARS "0123456789" + ALPHABET
|
||||
LOCAL cMsg As String, cCode As String
|
||||
LOCAL ARRAY codes[12]
|
||||
codes[1] = "710889"
|
||||
codes[2] = "B0YBKJ"
|
||||
codes[3] = "406566"
|
||||
codes[4] = "B0YBLH"
|
||||
codes[5] = "228276"
|
||||
codes[6] = "B0YBKL"
|
||||
codes[7] = "557910"
|
||||
codes[8] = "B0YBKR"
|
||||
codes[9] = "585284"
|
||||
codes[10] = "B0YBKT"
|
||||
codes[11] = "B00030"
|
||||
codes[12] = "B0030A"
|
||||
DIMENSION w[6]
|
||||
w[1] = 1
|
||||
w[2] = 3
|
||||
w[3] = 1
|
||||
w[4] = 7
|
||||
w[5] = 3
|
||||
w[6] = 9
|
||||
CLEAR
|
||||
FOR EACH cCode IN codes
|
||||
cMsg = ""
|
||||
IF IsValidCode(@cCode, @cMsg) && Parameters passed by reference
|
||||
cCode = cCode + GetCheckDigit(cCode)
|
||||
? cCode
|
||||
ELSE
|
||||
? cCode, cMsg
|
||||
ENDIF
|
||||
ENDFOR
|
||||
|
||||
FUNCTION GetCheckDigit(tcCode As String) As String
|
||||
LOCAL i As Integer, c As String, s As Integer, k As Integer
|
||||
s = 0
|
||||
FOR i = 1 TO 6
|
||||
c = SUBSTR(tcCode, i, 1)
|
||||
IF ISDIGIT(c)
|
||||
k = VAL(c)
|
||||
ELSE
|
||||
k = 9 + AT(c, ALPHABET)
|
||||
ENDIF
|
||||
s = s + k*w[i]
|
||||
ENDFOR
|
||||
RETURN TRANSFORM((10 - s%10)%10)
|
||||
ENDFUNC
|
||||
|
||||
FUNCTION IsValidCode(tcCode As String, tcMsg As String) As Boolean
|
||||
LOCAL n As Integer, c As String, i As Integer
|
||||
*!* Get rid of any spaces and convert to upper case
|
||||
tcCode = UPPER(STRTRAN(tcCode, " "))
|
||||
n = LEN(tcCode)
|
||||
IF LEN(tcCode) # 6
|
||||
tcMsg = "Code must be 6 characters."
|
||||
ELSE
|
||||
FOR i = 1 TO n
|
||||
c = SUBSTR(tcCode, i, 1)
|
||||
IF NOT c $ VALIDCHAR
|
||||
tcMsg = c + " is not a valid character."
|
||||
EXIT
|
||||
ELSE
|
||||
IF c $ VOWELS
|
||||
tcMsg = "Vowels are not allowed."
|
||||
EXIT
|
||||
ENDIF
|
||||
ENDIF
|
||||
ENDFOR
|
||||
ENDIF
|
||||
RETURN EMPTY(tcMsg)
|
||||
ENDFUNC
|
||||
43
Task/SEDOLs/jq/sedols.jq
Normal file
43
Task/SEDOLs/jq/sedols.jq
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
def ascii_upcase:
|
||||
explode | map( if 97 <= . and . <= 122 then . - 32 else . end) | implode;
|
||||
|
||||
def sedol_checksum:
|
||||
def encode(a): 10 + (a|explode[0]) - ("A"|explode[0]);
|
||||
. as $sed
|
||||
| [1,3,1,7,3,9] as $sw
|
||||
| reduce range(0;6) as $i
|
||||
(0;
|
||||
$sed[$i:$i+1] as $c
|
||||
| if ( "0123456789" | index($c) )
|
||||
then . + ($c|tonumber) * $sw[$i]
|
||||
else . + encode($c) * $sw[$i]
|
||||
end )
|
||||
| (10 - (. % 10)) % 10 ;
|
||||
|
||||
# error on error, else pass input to output
|
||||
def check_valid_sedol:
|
||||
def has_vowel:
|
||||
("AEIOU"|explode) as $vowels
|
||||
| reduce explode[] as $c
|
||||
(false; if . then . else $vowels|index($c) end);
|
||||
|
||||
if has_vowel then error( "\(.) is not a valid SEDOL code" )
|
||||
else .
|
||||
end
|
||||
| if length > 7 or length < 6 then
|
||||
error( "\(.) is too long or too short to be valid SEDOL")
|
||||
else .
|
||||
end;
|
||||
|
||||
def sedolize:
|
||||
ascii_upcase as $in
|
||||
| $in
|
||||
| check_valid_sedol
|
||||
| .[0:6] as $sedol
|
||||
| ($sedol | sedol_checksum | tostring) as $sedolcheck
|
||||
| ($sedol + $sedolcheck) as $ans
|
||||
| if length == 7 and $ans != $in then
|
||||
$ans + " (original \($in) has wrong checksum digit"
|
||||
else $ans
|
||||
end ;
|
||||
sedolize
|
||||
Loading…
Add table
Add a link
Reference in a new issue