40 lines
1.1 KiB
Text
40 lines
1.1 KiB
Text
sequence cch = {}
|
|
|
|
function CusipCheckDigit(string cusip)
|
|
integer s = 0, c, v
|
|
if length(cch)=0 then
|
|
cch = repeat(-1,256)
|
|
for i='0' to '9' do
|
|
cch[i] = i-'0'
|
|
end for
|
|
for i='A' to 'Z' do
|
|
cch[i] = i-55
|
|
end for
|
|
cch['*'] = 36
|
|
cch['@'] = 37
|
|
cch['#'] = 38
|
|
end if
|
|
if length(cusip)!=9 or find('\0',cusip) then return 0 end if
|
|
for i=1 to 8 do
|
|
c := cusip[i]
|
|
v := cch[c]
|
|
if v=-1 then return 0 end if
|
|
if remainder(i,2)=0 then
|
|
v *= 2
|
|
end if
|
|
s += floor(v/10)+mod(v,10)
|
|
end for
|
|
return cusip[9]=mod(10-mod(s,10),10)+'0'
|
|
end function
|
|
|
|
sequence tests = {"037833100", -- Apple Incorporated
|
|
"17275R102", -- Cisco Systems
|
|
"38259P508", -- Google Incorporated
|
|
"594918104", -- Microsoft Corporation
|
|
"68389X106", -- Oracle Corporation (incorrect)
|
|
"68389X105"} -- Oracle Corporation
|
|
|
|
for i=1 to length(tests) do
|
|
string ti = tests[i]
|
|
printf(1,"%s : %s\n",{ti,{"invalid","valid"}[CusipCheckDigit(ti)+1]})
|
|
end for
|