Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -4,7 +4,7 @@ F cusip_check(=cusip)
|
|||
|
||||
cusip = cusip.uppercase()
|
||||
V total = 0
|
||||
L(i) 8
|
||||
L(i) 0.<8
|
||||
V v = 0
|
||||
V c = cusip[i]
|
||||
I c.is_digit()
|
||||
|
|
|
|||
|
|
@ -1,44 +1,35 @@
|
|||
BEGIN
|
||||
# returns TRUE if cusip is a valid CUSIP code #
|
||||
OP ISCUSIP = ( STRING cusip )BOOL:
|
||||
IF ( UPB cusip - LWB cusip ) /= 8
|
||||
THEN
|
||||
# code is wrong length #
|
||||
FALSE
|
||||
ELSE
|
||||
# string is 9 characters long - check it is valid #
|
||||
STRING cusip digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#"[ AT 0 ];
|
||||
INT check digit := 0;
|
||||
IF NOT char in string( cusip[ UPB cusip ], check digit, cusip digits )
|
||||
THEN
|
||||
# invalid check digit #
|
||||
FALSE
|
||||
ELSE
|
||||
# OK so far compare the calculated check sum to the supplied one #
|
||||
INT sum := 0;
|
||||
INT c pos := LWB cusip - 1;
|
||||
FOR i TO 8 DO
|
||||
INT digit := 0;
|
||||
IF NOT char in string( cusip[ i + c pos ], digit, cusip digits )
|
||||
THEN
|
||||
# invalid digit #
|
||||
digit := -999
|
||||
FI;
|
||||
IF NOT ODD i
|
||||
THEN
|
||||
# even digit #
|
||||
digit *:= 2
|
||||
FI;
|
||||
sum +:= ( digit OVER 10 ) + ( digit MOD 10 )
|
||||
OD;
|
||||
( 10 - ( sum MOD 10 ) ) MOD 10 = check digit
|
||||
FI
|
||||
FI ; # ISCUSIP #
|
||||
BEGIN # validate CUSIP codes #
|
||||
|
||||
# returns TRUE if cusip string is a valid CUSIP code, FALSE otherwise #
|
||||
OP ISCUSIP = ( STRING cusip string )BOOL:
|
||||
IF UPB cusip string - LWB cusip string /= 8
|
||||
THEN FALSE # CUSIP code is not 9 characters long #
|
||||
ELSE # CUSIP code has the correct length #
|
||||
|
||||
# returns the base 39 digit corresponding to a character of a CUSIP code #
|
||||
PROC cusip digit = ( CHAR c char )INT:
|
||||
IF STRING cusip digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#"[ AT 0 ];
|
||||
INT digit := 0;
|
||||
char in string( c char, digit, cusip digits )
|
||||
THEN digit
|
||||
ELSE -999 # invalid digit #
|
||||
FI # cusip digit # ;
|
||||
|
||||
[]CHAR cusip = cusip string[ AT 1 ];
|
||||
INT sum := 0;
|
||||
INT check digit = cusip digit( cusip[ UPB cusip ] );
|
||||
FOR c pos TO UPB cusip - 1 DO
|
||||
INT digit := cusip digit( cusip[ c pos ] );
|
||||
IF NOT ODD c pos THEN digit *:= 2 FI;
|
||||
sum +:= ( digit OVER 10 ) + ( digit MOD 10 )
|
||||
OD;
|
||||
( 10 - ( sum MOD 10 ) ) MOD 10 = check digit
|
||||
FI # is cusip # ;
|
||||
|
||||
# task test cases #
|
||||
|
||||
PROC test cusip = ( STRING cusip )VOID:
|
||||
print( ( cusip, IF ISCUSIP cusip THEN " valid" ELSE " invalid" FI, newline ) );
|
||||
print( ( cusip, IF ISCUSIP cusip THEN " valid" ELSE " invalid" FI, newline ) );
|
||||
|
||||
test cusip( "037833100" );
|
||||
test cusip( "17275R102" );
|
||||
|
|
|
|||
|
|
@ -1,54 +0,0 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Cusip_Test is
|
||||
use Ada.Text_IO;
|
||||
|
||||
subtype Cusip is String (1 .. 9);
|
||||
|
||||
function Check_Cusip (Code : Cusip) return Boolean is
|
||||
Sum : Integer := 0;
|
||||
V : Integer;
|
||||
|
||||
begin
|
||||
for I in Code'First .. Code'Last - 1 loop
|
||||
case Code (I) is
|
||||
when '0' .. '9' =>
|
||||
V := Character'Pos (Code (I)) - Character'Pos ('0');
|
||||
when 'A' .. 'Z' =>
|
||||
V := Character'Pos (Code (I)) - Character'Pos ('A') + 10;
|
||||
when '*' => V := 36;
|
||||
when '@' => V := 37;
|
||||
when '#' => V := 38;
|
||||
when others => return False;
|
||||
end case;
|
||||
|
||||
if I mod 2 = 0 then
|
||||
V := V * 2;
|
||||
end if;
|
||||
|
||||
Sum := Sum + V / 10 + (V mod 10);
|
||||
end loop;
|
||||
|
||||
return (10 - (Sum mod 10)) mod 10 =
|
||||
Character'Pos (Code (Code'Last)) - Character'Pos ('0');
|
||||
end Check_Cusip;
|
||||
|
||||
type Cusip_Array is array (Natural range <>) of Cusip;
|
||||
|
||||
Test_Array : Cusip_Array :=
|
||||
("037833100",
|
||||
"17275R102",
|
||||
"38259P508",
|
||||
"594918104",
|
||||
"68389X106",
|
||||
"68389X105");
|
||||
begin
|
||||
for I in Test_Array'Range loop
|
||||
Put (Test_Array (I) & ": ");
|
||||
if Check_Cusip (Test_Array (I)) then
|
||||
Put_Line ("valid");
|
||||
else
|
||||
Put_Line ("not valid");
|
||||
end if;
|
||||
end loop;
|
||||
end Cusip_Test;
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
validCUSIP?: function [cusip][
|
||||
s: 0
|
||||
alpha: `A`..`Z`
|
||||
alpha: 'A'..'Z'
|
||||
|
||||
loop.with:'i chop cusip 'c [
|
||||
v: 0
|
||||
|
||||
case ø
|
||||
when? [numeric? c] -> v: to :integer to :string c
|
||||
when? [in? c alpha] -> v: (index alpha c) + 1 + 9
|
||||
when? [c = `*`] -> v: 36
|
||||
when? [c = `@`] -> v: 37
|
||||
when? [c = `#`] -> v: 38
|
||||
else []
|
||||
when.has: c [
|
||||
[|numeric?] -> v: to :integer to :string c
|
||||
[|in? alpha] -> v: (index alpha c) + 1 + 9
|
||||
[= '*'] -> v: 36
|
||||
[= '@'] -> v: 37
|
||||
[= '#'] -> v: 38
|
||||
]
|
||||
|
||||
if odd? i -> v: 2 * v
|
||||
|
||||
|
|
|
|||
|
|
@ -12,12 +12,10 @@ func check inp$ .
|
|||
elif c = 35
|
||||
v = 38
|
||||
.
|
||||
if i mod 2 = 0
|
||||
v *= 2
|
||||
.
|
||||
if i mod 2 = 0 : v *= 2
|
||||
sum += v div 10 + v mod 10
|
||||
.
|
||||
return if (10 - (sum mod 10)) mod 10 = number substr inp$ 9 1
|
||||
return if (10 - sum mod 10) mod 10 = number substr inp$ 9 1
|
||||
.
|
||||
for s$ in [ "037833100" "17275R102" "38259P508" "594918104" "68389X106" "68389X105" ]
|
||||
write s$ & " is "
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
val isCusip = fn(s) {
|
||||
if s is not string or len(s) != 9 {
|
||||
return false
|
||||
}
|
||||
val isCusip = fn(s string) {
|
||||
if len(s) != 9 : return false
|
||||
|
||||
val basechars = '0'..'9' ~ 'A'..'Z' ~ "*@#"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
val isCusip = fn(s) {
|
||||
if s is not string or len(s) != 9 {
|
||||
return false
|
||||
}
|
||||
val isCusip = fn(s string) {
|
||||
if len(s) != 9 : return false
|
||||
|
||||
val basechars = '0'..'9' ~ 'A'..'Z' ~ "*@#"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,42 +1,40 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cch</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
sequence cch = {}
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">CusipCheckDigit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">cusip</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">v</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cch</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">cch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">to</span> <span style="color: #008000;">'9'</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">cch</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #008000;">'0'</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'A'</span> <span style="color: #008080;">to</span> <span style="color: #008000;">'Z'</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">cch</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">55</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">cch</span><span style="color: #0000FF;">[</span><span style="color: #008000;">'*'</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">36</span>
|
||||
<span style="color: #000000;">cch</span><span style="color: #0000FF;">[</span><span style="color: #008000;">'@'</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">37</span>
|
||||
<span style="color: #000000;">cch</span><span style="color: #0000FF;">[</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">38</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cusip</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">9</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'\0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cusip</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">c</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">cusip</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">v</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">cch</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">v</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">cusip</span><span style="color: #0000FF;">[</span><span style="color: #000000;">9</span><span style="color: #0000FF;">]=</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)+</span><span style="color: #008000;">'0'</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
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
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"037833100"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- Apple Incorporated</span>
|
||||
<span style="color: #008000;">"17275R102"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- Cisco Systems</span>
|
||||
<span style="color: #008000;">"38259P508"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- Google Incorporated</span>
|
||||
<span style="color: #008000;">"594918104"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- Microsoft Corporation</span>
|
||||
<span style="color: #008000;">"68389X106"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- Oracle Corporation (incorrect)</span>
|
||||
<span style="color: #008000;">"68389X105"</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- Oracle Corporation</span>
|
||||
sequence tests = {"037833100", -- Apple Incorporated
|
||||
"17275R102", -- Cisco Systems
|
||||
"38259P508", -- Google Incorporated
|
||||
"594918104", -- Microsoft Corporation
|
||||
"68389X106", -- Oracle Corporation (incorrect)
|
||||
"68389X105"} -- Oracle Corporation
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"invalid"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"valid"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">CusipCheckDigit</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
for i=1 to length(tests) do
|
||||
string ti = tests[i]
|
||||
printf(1,"%s : %s\n",{ti,{"invalid","valid"}[CusipCheckDigit(ti)+1]})
|
||||
end for
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
function Get-CheckDigitCUSIP {
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param ( # Validate input
|
||||
[Parameter(Mandatory=$true, Position=0)]
|
||||
[ValidatePattern( '^[A-Z0-9@#*]{8}\d$' )] # @#*
|
||||
[ValidateScript({$_.Length -eq 9})]
|
||||
[string]
|
||||
$cusip
|
||||
)
|
||||
$sum = 0
|
||||
0..7 | ForEach { $c = $cusip[$_] ; $v = $null
|
||||
if ([Char]::IsDigit($c)) { $v = [char]::GetNumericValue($c) }
|
||||
if ([Char]::IsLetter($c)) { $v = [int][char]$c - [int][char]'A' +10 }
|
||||
if ($c -eq '*') { $v = 36 }
|
||||
if ($c -eq '@') { $v = 37 }
|
||||
if ($c -eq '#') { $v = 38 }
|
||||
if($_ % 2){ $v += $v }
|
||||
$sum += [int][Math]::Floor($v / 10 ) + ($v % 10)
|
||||
}
|
||||
[int]$checkDigit_calculated = ( 10 - ($sum % 10) ) % 10
|
||||
return( $checkDigit_calculated )
|
||||
}
|
||||
|
||||
function Test-IsCUSIP {
|
||||
[CmdletBinding()]
|
||||
[OutputType([bool])]
|
||||
Param (
|
||||
[Parameter(Mandatory=$true, Position=0)]
|
||||
[ValidatePattern( '^[A-Z0-9@#*]{8}\d$' )]
|
||||
[ValidateScript({$_.Length -eq 9})]
|
||||
[string]
|
||||
$cusip
|
||||
)
|
||||
[int]$checkDigit_told = $cusip[-1].ToString()
|
||||
$checkDigit_calculated = Get-CheckDigitCUSIP $cusip
|
||||
($checkDigit_calculated -eq $checkDigit_told)
|
||||
}
|
||||
|
||||
$data = @"
|
||||
037833100`tApple Incorporated
|
||||
17275R102`tCisco Systems
|
||||
38259P508`tGoogle Incorporated
|
||||
594918104`tMicrosoft Corporation
|
||||
68389X106`tOracle Corporation (incorrect)
|
||||
68389X105`tOracle Corporation
|
||||
"@ -split "`n"
|
||||
$data |%{ Test-IsCUSIP $_.Split("`t")[0] }
|
||||
Loading…
Add table
Add a link
Reference in a new issue