CUSIP

From Rosetta Code
Revision as of 22:15, 11 February 2017 by Thundergnat (talk | contribs) (→‎{{header|Perl 6}}: Add a Perl 6 example)
CUSIP is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at CUSIP. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)


A CUSIP is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.

The task here is to ensure the last digit (i.e. check digit) of the CUSIP code is correct, against the following codes.

  • Apple Inc.: 037833100
  • Cisco Systems: 17275R102
  • Google Inc.: 38259P508
  • Microsoft Corporation: 594918104
  • Oracle Corporation (Incorrect): 68389X106
  • Oracle Corporation: 68389X105


Example pseudo-code below.

<lang>algorithm Cusip-Check-Digit(cusip) is

  Input: an 8-character CUSIP
  sum := 0
  for 1 ≤ i ≤ 8 do
     c := the ith character of cusip
     if c is a digit then
        v := numeric value of the digit c
     else if c is a letter then
        p := ordinal position of c in the alphabet (A=1, B=2...)
        v := p + 9
     else if c = "*" then
        v := 36
     else if c = "@" then
        v := 37
     else if' c = "#" then
        v := 38
     end if
     if i is even then
        v := v × 2
     end if
     sum := sum + int ( v div 10 ) + v mod 10
  repeat
  
  return (10 - (sum mod 10)) mod 10

end function</lang>

See also

Caché ObjectScript

<lang cos>Class Utils.Check [ Abstract ] {

ClassMethod CUSIP(x As %String) As %Boolean { SET x=$TRANSLATE(x," ") // https://leiq.bus.umich.edu/res_codes_cusip.htm IF x'?8UNP1N QUIT 0 SET cd=$EXTRACT(x,*), x=$EXTRACT(x,1,*-1), t=0 FOR i=1:1:$LENGTH(x) { SET n=$EXTRACT(x,i) IF n'=+n SET n=$CASE(n,"*":36,"@":37,"#":38,:$ASCII(n)-55) IF i#2=0 SET n=n*2 SET t=t+(n\10)+(n#10) } QUIT cd=((10-(t#10))#10) }

}</lang>

Examples:
USER>For  { Read s Quit:s=""  Write ": "_##class(Utils.Check).CUSIP(s), ! }         
037833100: 1
17275R102: 1
38259P508: 1
594918104: 1
68389X106: 0
68389X105: 1

USER>

Perl 6

Works with: Rakudo version 2017.01

<lang perl6>sub divmod ($v, $r) { $v div $r, $v mod $r } my %chr = (flat 0..9, 'A'..'Z', <* @ #>) Z=> 0..*;

sub cuisp-check ($cuisp where *.chars == 9) {

   my ($code, $chk) = $cuisp.comb(8);
   my $sum = [+] $code.comb.kv.map: { [+] (($^k % 2 + 1) * %chr{$^v}).&divmod(10) };
   so (10 - $sum mod 10) mod 10 eq $chk;

}

  1. TESTING

say "$_: ", $_.&cuisp-check for < 037833100 17275R102 38259P508 594918104 68389X106 68389X105 ></lang>

Output:
037833100: True
17275R102: True
38259P508: True
594918104: True
68389X106: False
68389X105: True