CUSIP: Difference between revisions

2,142 bytes added ,  1 year ago
m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 17:
 
;Example pseudo-code below.
<syntaxhighlight lang=text>algorithm Cusip-Check-Digit(cusip) is
Input: an 8-character CUSIP
 
Line 43:
return (10 - (sum mod 10)) mod 10
end function</langsyntaxhighlight>
 
;See related tasks:
Line 53:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F cusip_check(=cusip)
I cusip.len != 9
X ValueError(‘CUSIP must be 9 characters’)
Line 88:
‘68389X105’]
L(code) codes
print(code‘: ’(I cusip_check(code) {‘valid’} E ‘invalid’))</langsyntaxhighlight>
 
{{out}}
Line 101:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang=360asm>* CUSIP 07/06/2018
CUSIP CSECT
USING CUSIP,R13 base register
Line 181:
PG DC CL80'CUSIP ......... is... valid'
YREGS
END CUSIP</langsyntaxhighlight>
{{out}}
<pre>
Line 194:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight lang=Action!>INCLUDE "D2:CHARTEST.ACT" ;from the Action! Tool Kit
 
BYTE FUNC Verify(CHAR ARRAY code)
Line 254:
Test("68389X106")
Test("68389X105")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/CUSIP.png Screenshot from Atari 8-bit computer]
Line 267:
 
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>with Ada.Text_IO;
 
procedure Cusip_Test is
Line 320:
end if;
end loop;
end Cusip_Test;</langsyntaxhighlight>
{{Out}}
<pre>037833100: valid
Line 331:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang=algol68>BEGIN
# returns TRUE if cusip is a valid CUSIP code #
OP ISCUSIP = ( STRING cusip )BOOL:
Line 379:
test cusip( "68389X106" );
test cusip( "68389X105" )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 392:
=={{header|ALGOL W}}==
Based on Algol 68
<langsyntaxhighlight lang=algolw>begin % returns true if cusip is a valid CUSIP code %
logical procedure isCusip ( string(9) value cusip ) ;
begin
Line 426:
testCusip( "68389X105" )
end testCases
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 438:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang=applescript>use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 717:
return lst
end tell
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>037833100 -> true
Line 728:
=={{header|Arturo}}==
{{trans|Ruby}}
<langsyntaxhighlight lang=rebol>validCUSIP?: function [cusip][
s: 0
alpha: `A`..`Z`
Line 754:
loop ["037833100" "17275R102" "38259P508" "594918104" "68389X106" "68389X105"] 'cusip [
print [cusip "=>" (validCUSIP? cusip)? -> "VALID" -> "INVALID"]
]</langsyntaxhighlight>
 
{{out}}
Line 766:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>Cusip_Check_Digit(cusip){
sum := 0, i := 1, x := StrSplit(cusip)
while (i <= 8) {
Line 786:
}
return (Mod(10 - Mod(sum, 10), 10) = x[9])
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight lang=AutoHotkey>data =
(
037833100
Line 800:
loop, Parse, data, `n, `r
output .= A_LoopField "`t" Cusip_Check_Digit(A_LoopField) "`n"
MsgBox % output</langsyntaxhighlight>
{{out}}
<pre>Cusip Valid
Line 811:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CUSIP.AWK
BEGIN {
Line 853:
return(substr(n,9,1) == x ? 1 : 0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 865:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang=bcpl>get "libhdr"
 
let validcusip(c) = valof
Line 899:
show("68389X106")
show("68389X105")
$)</langsyntaxhighlight>
{{out}}
<pre>037833100: valid
Line 910:
=={{header|C}}==
Reads CUSIP strings from a file and prints results to console, usage printed on incorrect invocation.
<syntaxhighlight lang=C>
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 964:
return 0;
}
</syntaxhighlight>
</lang>
Input file :
<pre>
Line 990:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
 
Line 1,037:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>037833100 -> correct
Line 1,048:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang=cpp>#include <iostream>
#include <vector>
 
Line 1,096:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>037833100 -> correct
Line 1,107:
=={{header|Caché ObjectScript}}==
 
<langsyntaxhighlight lang=cos>Class Utils.Check [ Abstract ]
{
 
Line 1,125:
}
 
}</langsyntaxhighlight>
{{out|Examples}}
<pre>USER>For { Read s Quit:s="" Write ": "_##class(Utils.Check).CUSIP(s), ! }
Line 1,138:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang=Clojure>
(defn- char->value
"convert the given char c to a value used to calculate the cusip check sum"
Line 1,185:
"EXTRACRD9" "BADCUSIP!" "683&9X106" "68389x105" "683$9X106" "68389}105" "87264ABE4"]]
(println cusip (if (is-valid-cusip9? cusip) "valid" "invalid"))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,207:
 
=={{header|CLU}}==
<langsyntaxhighlight lang=clu>valid_cusip = proc (s: string) returns (bool)
own chars: string := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#"
if string$size(s) ~= 9 then return(false) end
Line 1,239:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>037833100: valid
Line 1,249:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang=lisp>(defun char->value (c)
(cond ((digit-char-p c 36))
((char= c #\*) 36)
Line 1,271:
(defun main ()
(dolist (cusip '("037833100" "17275R102" "38259P508" "594918104" "68389X106" "68389X105"))
(format t "~A: ~A~%" cusip (cusip-p cusip))))</langsyntaxhighlight>
{{out}}
<pre>037833100: T
Line 1,281:
 
=={{header|D}}==
<langsyntaxhighlight lang=D>import std.stdio;
 
void main(string[] args) {
Line 1,379:
}
 
/// Invoke with `cusip 037833100 17275R102 38259P508 594918104 68389X106 68389X105`</langsyntaxhighlight>
 
{{out}}
Line 1,394:
{{trans|Go}}
 
<langsyntaxhighlight lang=dyalect>func isCusip(s) {
if s.Length() != 9 { return false }
var sum = 0
Line 1,431:
}
print("\(candidate) -> \(b)")
}</langsyntaxhighlight>
 
{{out}}
Line 1,449:
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
<langsyntaxhighlight lang=lisp>=LAMBDA(s,
LET(
ns, VLOOKUP(
Line 1,487:
10;"B",11;"C",12;"D",13;"E",14;"F",15;"G",16;"H",17;"I",18;"J",19;"K",
20;"L",21;"M",22;"N",23;"O",24;"P",25;"Q",26;"R",27;"S",28;"T",29;"U",
30;"V",31;"W",32;"X",33;"Y",34;"Z",35;"*",36;"@",37;"#",38}</langsyntaxhighlight>
 
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<langsyntaxhighlight lang=lisp>CHARS
=LAMBDA(s,
MID(
Line 1,521:
COLUMNS(xs)
)
)</langsyntaxhighlight>
 
{{Out}}
Line 1,563:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// Validate CUSIP: Nigel Galloway. June 2nd., 2021
let fN=function n when n>47 && n<58->n-48 |n when n>64 && n<91->n-55 |42->36 |64->37 |_->38
let cD(n:string)=(10-(fst((n.[0..7])|>Seq.fold(fun(z,n)g->let g=(fN(int g))*(n+1) in (z+g/10+g%10,(n+1)%2))(0,0)))%10)%10=int(n.[8])-48
["037833100";"17275R102";"38259P508";"594918104";"68389X103";"68389X105"]|>List.iter(fun n->printfn "CUSIP %s is %s" n (if cD n then "valid" else "invalid"))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,580:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: combinators.short-circuit formatting kernel math
math.parser qw regexp sequences unicode ;
IN: rosetta-code.cusip
Line 1,597:
 
qw{ 037833100 17275R102 38259P508 594918104 68389X106 68389X105 }
[ dup cusip? "correct" "incorrect" ? "%s -> %s\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,613:
The source does not bother with the MODULE protocol of F90 and later, and so the type of function CUSIPCHECK must be declared in all routines wishing to invoke it. However, the F90 feature of having the END statement of a subroutine or function give its name is to valuable to ignore. The function returns a character code rather than an integer, since the presumption is that it is to be compared to the check character of the code being inspected, which is known as a character not an integer. This means some blather when extracting the eight characters to be presented to CUSIPCHECK and comparing the result to the ninth character, but the test can be done in one expression.
 
There is no checking that only valid characters are presented, nor that eight-character codes only are offered, though the compiler might complain if the function were to be invoked with a text literal of the wrong size. In the absence of such checks, there need be no added complications to support a scheme for reporting such errors. <langsyntaxhighlight lang=Fortran> CHARACTER*1 FUNCTION CUSIPCHECK(TEXT) !Determines the check sum character.
Committee on Uniform Security Identification Purposes, of the American (i.e. USA) Bankers' Association.
CHARACTER*8 TEXT !Specifically, an eight-symbol code.
Line 1,646:
END DO
 
END</langsyntaxhighlight>
 
Output: standard output is to I/O unit 6, and free-format (the *) will suffice for this. Each line output starts with a space (in case it is to go to a lineprinter, with carriage control), which is convenient for layout here.
Line 1,659:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>' version 04-04-2017
' compile with: fbc -s console
 
Line 1,721:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>037833100 is valid
Line 1,731:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,781:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,795:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=groovy>class Cusip {
private static Boolean isCusip(String s) {
if (s.length() != 9) return false
Line 1,834:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>037833100 -> correct
Line 1,844:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Data.List(elemIndex)
 
data Result = Valid | BadCheck | TooLong | TooShort | InvalidContent deriving Show
Line 1,887:
]
 
main = mapM_ putStrLn (fmap (\s -> s ++ ": " ++ show (checkCUSIP s)) testData)</langsyntaxhighlight>
 
{{out}}
Line 1,899:
 
Or, picking some other possibilities from Haskell's rich libraries:
<langsyntaxhighlight lang=Haskell>import qualified Data.Map as M (Map, fromList, lookup)
import Data.Maybe (fromMaybe)
 
Line 1,935:
"68389X106",
"68389X105"
]</langsyntaxhighlight>
{{Out}}
<pre>("037833100",True)
Line 1,945:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang=Icon># cusip.icn -- Committee on Uniform Security Identification Procedures
 
procedure main()
Line 1,984:
t[chars[n]] := (n - 1)
return t
end</langsyntaxhighlight>
 
{{out}}<pre>037833100 : valid.
Line 1,996:
=={{header|J}}==
One-liner:
<langsyntaxhighlight lang=j> ccd =. 10 | 10 - 10 | [: +/ [: , 10 (#.^:_1) (8 $ 1 2) * '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#' i. ]
ccd '68389X10'
5</langsyntaxhighlight>
 
More verbose version that checks for correct input:
<langsyntaxhighlight lang=j> CUSIPcheckdigit =. 3 : 0
assert. 8 = $ y NB. Only accept an 8-element long list
assert. */ y e. '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#' NB. Only accept characters from the list of 38
Line 2,010:
)
addCUSIPcheckdigit =: , CUSIPcheckdigit
verifyCUSIPcheckdigit =: {: = CUSIPcheckdigit@}:</langsyntaxhighlight>
 
Examples:
<langsyntaxhighlight lang=j> addCUSIPcheckdigit '68389X10'
68389X105
verifyCUSIPcheckdigit '68389X106'
Line 2,028:
│68389X106│0│
│68389X105│1│
└─────────┴─┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
Uses Java 9
<langsyntaxhighlight lang=Java>import java.util.List;
 
public class Cusip {
Line 2,071:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>037833100 -> correct
Line 2,090:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=javascript>(() => {
'use strict';
 
Line 2,518:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>037833100 -> true
Line 2,530:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang=julia>module CUSIP
 
function _lastdigitcusip(input::AbstractString)
Line 2,562:
for code in ("037833100", "17275R102", "38259P508", "594918104", "68389X106", "68389X105")
println("$code is ", CUSIP.checkdigit(code) ? "correct." : "not correct.")
end</langsyntaxhighlight>
 
{{out}}
Line 2,573:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// version 1.1.0
 
fun isCusip(s: String): Boolean {
Line 2,605:
for (candidate in candidates)
println("$candidate -> ${if(isCusip(candidate)) "correct" else "incorrect"}")
}</langsyntaxhighlight>
 
{{out}}
Line 2,621:
 
{{works with|langur|0.8.5}}
<langsyntaxhighlight lang=langur>val .isCusip = f(.s) {
if not isString(.s) or len(.s) != 9 {
return false
Line 2,643:
for .c in .candidates {
writeln .c, ": ", if(.isCusip(.c): "good" ; "bad")
}</langsyntaxhighlight>
 
Following the pseudo-code would look more like the following.
Line 2,649:
{{works with|langur|0.8.5}}
{{trans|Go}}
<langsyntaxhighlight lang=langur>val .isCusip = f(.s) {
if not isString(.s) or len(.s) != 9 {
return false
Line 2,685:
for .c in .candidates {
writeln .c, ": ", if(.isCusip(.c): "good" ; "bad")
}</langsyntaxhighlight>
 
{{out}}
Line 2,697:
=={{header|Lua}}==
The checkDigit function is a line-for-line translation of the pseudo-code algorithm.
<langsyntaxhighlight lang=Lua>function checkDigit (cusip)
if #cusip ~= 8 then return false end
Line 2,740:
print("INVALID")
end
end</langsyntaxhighlight>
{{out}}
<pre>037833100: VALID
Line 2,750:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[Cusip]
rules = Thread[(ToString /@ Range[0, 9]) -> Range[0, 9]]~Join~
Thread[CharacterRange["A", "Z"] -> Range[26] + 9]~Join~
Line 2,772:
]
]
Cusip /@ {"037833100", "17275R102", "38259P508", "594918104", "68389X106", "68389X105"}</langsyntaxhighlight>
{{out}}
<pre>{True, True, True, True, False, True}</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang=modula2>MODULE CUSIP;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,852:
 
ReadChar
END CUSIP.</langsyntaxhighlight>
{{out}}
<pre>CUSIP Verdict
Line 2,863:
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang=Nanoquery>def cusip_checksum(cusip)
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
num = "0123456789"
Line 2,900:
end
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,911:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=Nim>import strutils
 
proc cusipCheck(cusip: string): bool =
Line 2,951:
echo code, ": ", if cusipCheck(code): "Valid" else: "Invalid"
 
main()</langsyntaxhighlight>
 
{{out}}
Line 2,964:
=={{header|Objeck}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=Objeck>class Cusip {
function : native : IsCusip(s : String) ~ Bool {
if(s->Size() <> 9) {
Line 3,021:
};
}
}</langsyntaxhighlight>
 
Output:
Line 3,035:
=={{header|Perl}}==
 
<langsyntaxhighlight lang=perl>$cv{$_} = $i++ for '0'..'9', 'A'..'Z', '*', '@', '#';
 
sub cusip_check_digit {
Line 3,061:
);
 
print "$_ $test_data{$_}" . cusip_check_digit($_) . "\n" for sort keys %test_data;</langsyntaxhighlight>
{{out}}
<pre>037833100 Apple Incorporated
Line 3,071:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cch</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 3,112:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,124:
 
=={{header|PHP}}==
<langsyntaxhighlight lang=PHP>function IsCusip(string $s) {
if (strlen($s) != 9) return false;
$sum = 0;
Line 3,162:
"68389X105");
 
foreach ($cusips as $cusip) echo $cusip . " -> " . (IsCusip($cusip) ? "valid" : "invalid") . "\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 3,173:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(de cusip (Str)
(let (Str (mapcar char (chop Str)) S 0)
(for (I . C) (head 8 Str)
Line 3,201:
"38259P508"
"68389X106"
"68389X105" ) ) )</langsyntaxhighlight>
{{out}}
<pre>(T T T NIL T)</pre>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang=PowerShell>
function Get-CheckDigitCUSIP {
[CmdletBinding()]
Line 3,255:
"@ -split "`n"
$data |%{ Test-IsCUSIP $_.Split("`t")[0] }
</langsyntaxhighlight>{{out}}<pre>
True
True
Line 3,268:
Requires Python 3.6 for the string template literal in the print statement.
 
<langsyntaxhighlight lang=python>#!/usr/bin/env python3
 
import math
Line 3,310:
for code in codes:
print(f'{code} -> {cusip_check(code)}')
</syntaxhighlight>
</lang>
Output:
<pre>037833100 -> True
Line 3,323:
{{Works with|Python|3.7}}
Composing a set of pure functions, including a number of general and reusable abstractions:
<langsyntaxhighlight lang=python>'''CUSIP'''
 
from itertools import (cycle, islice, starmap)
Line 3,482:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Test for validity as a CUSIP string:
Line 3,495:
=={{header|Quackery}}==
 
<langsyntaxhighlight lang=Quackery> [ -1 split 0 peek char 0 -
swap 0 swap
witheach
Line 3,520:
$ "037833100 17275R102 38259P508 594918104 68389X106 68389X105"
nest$ witheach task</langsyntaxhighlight>
 
{{out}}
Line 3,534:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
(require srfi/14)
 
Line 3,570:
(check-true (CUSIP? "594918104"))
(check-false (CUSIP? "68389X106"))
(check-true (CUSIP? "68389X105")))</langsyntaxhighlight>
 
no output indicates all tests passed.
Line 3,578:
{{works with|Rakudo|2017.01}}
 
<syntaxhighlight lang=raku perl6line>sub divmod ($v, $r) { $v div $r, $v mod $r }
my %chr = (flat 0..9, 'A'..'Z', <* @ #>) Z=> 0..*;
 
Line 3,595:
68389X106
68389X105
></langsyntaxhighlight>
{{out}}
<pre>037833100: True
Line 3,606:
=={{header|REXX}}==
===idiomatic===
<langsyntaxhighlight lang=rexx>/*REXX program validates that the last digit (the check digit) of a CUSIP is valid. */
@.=
parse arg @.1 .
Line 3,637:
$=$ + #%10 + #//10
end /*k*/
return (10- $//10) // 10</langsyntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
Line 3,649:
 
===conciser function===
<langsyntaxhighlight lang=rexx>/*REXX program validates that the last digit (the check digit) of a CUSIP is valid. */
@.=
parse arg @.1 .
Line 3,674:
$=$ + #%10 + #//10
end /*k*/
return (10-$//10) // 10</langsyntaxhighlight>
'''output''' &nbsp; is the same as the idiomatic REXX version. <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : CUSIP
 
Line 3,736:
see inputstr + " is invalid" + nl
ok
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,749:
=={{header|Ruby}}==
===Following pseudocode===
<langsyntaxhighlight lang=ruby>
#!/usr/bin/env ruby
 
Line 3,789:
end
 
</syntaxhighlight>
</lang>
 
Output:
Line 3,802:
===More concise===
Since it uses methods like chain, to_h, sum, and infinite Range syntax (0..), this needs a Ruby version > 2.5
<langsyntaxhighlight lang=Ruby>
TABLE = ("0".."9").chain("A".."Z", %w(* @ #)).zip(0..).to_h
 
Line 3,814:
CUSIPs = %w(037833100 17275R102 38259P508 594918104 68389X106 68389X105)
CUSIPs.each{|cusip| puts "#{cusip}: #{valid_CUSIP? cusip}"}
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
 
<langsyntaxhighlight lang=rust>fn cusip_check(cusip: &str) -> bool {
if cusip.len() != 9 {
return false;
Line 3,859:
println!("{} -> {}", code, cusip_check(code))
}
}</langsyntaxhighlight>
 
Output:
Line 3,872:
=={{header|Scala}}==
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/jwxwWpq/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/OBrz9l14Rm2C6tV8tiwhWg Scastie (JVM)].
<langsyntaxhighlight lang=Scala>object Cusip extends App {
 
val candidates = Seq("037833100", "17275R102", "38259P508", "594918104", "68389X106", "68389X105")
Line 3,899:
}
 
}</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
<langsyntaxhighlight lang=snobol4>#!/usr/local/bin/snobol4 -r
* cusip.sno
* -- Committee on Uniform Security Identification Procedures
Line 3,951:
68389X10
68389X1059
68389x105</langsyntaxhighlight>
{{out}}
<pre>037833100 valid.
Line 3,965:
=={{header|Swift}}==
 
<langsyntaxhighlight lang=swift>struct CUSIP {
var value: String
 
Line 4,031:
print("Valid")
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,044:
=={{header|Tcl}}==
=== Direct translation of pseudocode ===
<langsyntaxhighlight lang=Tcl>proc ordinal-of-alpha {c} { ;# returns ordinal position of c in the alphabet (A=1, B=2...)
lsearch {_ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} [string toupper $c]
}
Line 4,081:
set cusip [string range $cusip 0 end-1]
expr {$last eq [Cusip-Check-Digit $cusip]}
}</langsyntaxhighlight>
 
=== More idiomatic Tcl ===
<langsyntaxhighlight lang=Tcl>proc check-cusip {code} {
if {[string length $code] != 9} {
return false
Line 4,100:
}
expr {$sum % 10 == 0}
}</langsyntaxhighlight>
 
=== Common test harness ===
<langsyntaxhighlight lang=Tcl>proc test {} {
foreach {cusip name} {
037833100 "Apple Incorporated"
Line 4,116:
}
}
test</langsyntaxhighlight>
 
=== Output ===
Line 4,128:
 
=={{header|VBA}}==
<langsyntaxhighlight lang=vb>Private Function Cusip_Check_Digit(s As Variant) As Integer
Dim Sum As Integer, c As String, v As Integer
For i = 1 To 8
Line 4,154:
Next i
Cusip_Check_Digit = (10 - (Sum Mod 10)) Mod 10
End Function</langsyntaxhighlight>{{out}}
<pre>037833100 is valid
17275R102 is valid
Line 4,164:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Module Module1
 
Function IsCUSIP(s As String) As Boolean
Line 4,211:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>037833100 -> correct
Line 4,222:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>fn is_cusip(s string) bool {
if s.len != 9 { return false }
mut sum := 0
Line 4,273:
println("$candidate -> $b")
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,287:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight lang=ecmascript>var isCusip = Fn.new { |s|
if (s.count != 9) return false
var sum = 0
Line 4,323:
var b = (isCusip.call(candidate)) ? "correct" : "incorrect"
System.print("%(candidate) -> %(b)")
}</langsyntaxhighlight>
 
{{out}}
Line 4,336:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>string 0; \use zero-terminated strings
 
func Valid(Cusip); \Return 'true' if valid CUSIP code
Line 4,374:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,388:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang=Yabasic>sub cusip(inputStr$)
local i, v, sum, x$
Line 4,435:
cusip(inputStr$)
loop
</syntaxhighlight>
</lang>
 
=={{header|Zig}}==
<langsyntaxhighlight lang=zig>const std = @import("std");
const print = std.debug.print;
 
Line 4,487:
}
}
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
<langsyntaxhighlight lang=zkl>fcn cusipCheckDigit(cusip){
var [const] vs=[0..9].chain(["A".."Z"],T("*","@","#")).pump(String);
try{
Line 4,497:
((10 - sum%10)%10 == cusip[8].toInt()) and cusip.len()==9
}catch{ False }
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>foreach cusip in (T("037833100", "17275R102",
"38259P508", "594918104", "68389X106", "68389X105")){
println(cusip,": ",cusipCheckDigit(cusip));
}</langsyntaxhighlight>
{{out}}
<pre>
10,339

edits