Validate International Securities Identification Number: Difference between revisions
Validate International Securities Identification Number (view source)
Revision as of 19:57, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 68:
{{trans|Python}}
<
I a.len != 12
R 0B
Line 91:
print([‘US0378331005’, ‘US0373831005’, ‘U50378331005’, ‘US03378331005’,
‘AU0000XVGZA3’, ‘AU0000VXGZA3’, ‘FR0000988040’].map(s -> check_isin(s)))</
{{out}}
Line 99:
=={{header|360 Assembly}}==
<
VALISIN CSECT
USING VALISIN,R13 base register
Line 274:
XDEC DS CL12 temp for xdeco and xdeci
REGEQU
END VALISIN</
{{out}}
<pre>
Line 289:
Calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task.
<
-- Luhn_Test copied from other Task
function Luhn_Test (Number: String) return Boolean is
Line 362:
when others =>
Ada.Text_IO.Put_Line("Exception occured");
end ISIN;</
Output:
Line 375:
=={{header|ALGOL W}}==
Uses the LuhnTest procedure from the [[Luhn test of credit card numbers]] task.
<
% external procedure that returns true if ccNumber passes the Luhn test, false otherwise %
logical procedure LuhnTest ( string(32) value ccNumber
Line 449:
testIsIsin( "AU0000VXGZA3", true );
testIsIsin( "FR0000988040", true );
end.</
{{out}}
<pre>
Line 465:
This script calls a handler posted for the [https://www.rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Straightforward Luhn test of credit card numbers] task.
<
use framework "Foundation"
Line 490:
set end of testResults to {testNumber:ISIN's contents, valid:ISINTest(ISIN)}
end repeat
return testResults</
{{output}}
<
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f VALIDATE_INTERNATIONAL_SECURITIES_IDENTIFICATION_NUMBER.AWK
# converted from Fortran
Line 532:
return(v % 10 == 0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 545:
=={{header|C}}==
<
int check_isin(char *a) {
Line 590:
}
/* will print: T F F F T T T */</
=={{header|C sharp|C#}}==
{
<
using System.Linq;
using System.Text.RegularExpressions;
Line 644:
}
}
}</
{{out}}
<pre>US0378331005 is valid
Line 655:
=={{header|C++}}==
<
#include <string>
Line 716:
return 0;
}
</syntaxhighlight>
=={{header|Caché ObjectScript}}==
<
{
Line 749:
}
}</
{{out|Examples}}
<pre>USER>For { Read isin Quit:isin="" Write ": "_##class(Utils.Check).ISIN(isin), ! }
Line 763:
=={{header|Clojure}}==
<
(let [sum (->> cc
(map #(Character/digit ^char % 10))
Line 783:
"AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"]]
(cl-format *out* "~A: ~:[invalid~;valid~]~%" isin (is-valid-isin? isin)))
</syntaxhighlight>
<tt>luhn?</tt> is based on ''[[Luhn test of credit card numbers#Clojure]]''.
{{out}}
Line 796:
=={{header|COBOL}}==
{{works with|GnuCOBOL}}
<
*> this is gnucobol 2.0
identification division.
Line 968:
goback
.
end program luhntest.</
{{out}}
Line 981:
=={{header|Common Lisp}}==
<
(char<= #\A char #\Z))
Line 1,018:
(dolist (isin '("US0378331005" "US0373831005" "U50378331005" "US03378331005"
"AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"))
(format t "~A: ~:[invalid~;valid~]~%" isin (valid-isin-p isin))))</
{{out}}
<pre>US0378331005: valid
Line 1,031:
{{trans|Java}}
Code for the luhn test was taken from [[https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#D]]
<
void main() {
Line 1,070:
import luhn;
return luhnTest(sb.data);
}</
{{out}}
Line 1,083:
=={{header|Elixir}}==
used Luhn module from [[Luhn_test_of_credit_card_numbers#Elixir | here]]
<
if str =~ ~r/\A[A-Z]{2}[A-Z0-9]{9}\d\z/ do
String.codepoints(str)
Line 1,101:
AU0000VXGZA3
FR0000988040)
|> Enum.each(&IO.puts "#{&1}\t#{isin?.(&1)}")</
{{out}}
Line 1,117:
=={{header|Factor}}==
We re-use the <code>luhn?</code> word from ''[[Luhn test of credit card numbers#Factor]]''.
<
math math.parser qw sequences strings unicode ;
IN: rosetta-code.isin
Line 1,154:
] each ;
MAIN: main</
{{out}}
<pre>
Line 1,168:
=={{header|Fortran}}==
<
use ctype
implicit none
Line 1,224:
check_isin = 0 == mod(v, 10)
end function
end program</
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,304:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>US0378331005 Valid
Line 1,316:
=={{header|Go}}==
<
import "regexp"
Line 1,345:
sum += int(n[11] - '0')
return sum%10 == 0
}</
<
import "testing"
Line 1,372:
}
}
}</
=={{header|Groovy}}==
Line 1,378:
{{update|Groovy|Use the new test-cases, and consider calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task instead of duplicating it.}}
<
int checksum(String prefix) {
Line 1,390:
assert checksum('GB000263494') == 6
assert checksum('US037833100') == 5
assert checksum('US037833107') == 0</
=={{header|Haskell}}==
<
import Data.Char (isUpper, isDigit, digitToInt)
Line 1,474:
, "FR0000988040"
]
mapM_ printSolution isinnumbers</
{{out}}
Line 1,486:
Or, making alternative choices from the standard libraries:
<
import Data.Bifunctor (first)
import Data.List (foldl') -- '
Line 1,550:
"AU0000VXGZA3",
"FR0000988040"
]</
{{Out}}
<pre>("US0378331005",True)
Line 1,563:
'''Solution:'''
<
validFmt=: 0 -: '^[A-Z]{2}[A-Z0-9]{9}[0-9]{1}$'&rxindex
Line 1,569:
luhn=: 0 = 10 (| +/@,) 10 #.inv 1 2 *&|: _2 "."0\ |. NB. as per task Luhn_test_of_credit_card_numbers#J
validISIN=: validFmt *. luhn@df36</
'''Required Examples:'''
<
validISIN&> Tests
1 0 0 0 1 1 1</
=={{header|Java}}==
As the Luhn test method from the ''[[Luhn test of credit card numbers]]'' task is only a few lines, it has been embedded in the ISIN class for convenience.
<
public static void main(String[] args) {
Line 1,625:
return (s1 + s2) % 10 == 0;
}
}</
<pre>US0378331005 is valid
Line 1,638:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<
def luhntest:
def digits: tostring | explode | map([.]|implode|tonumber);
Line 1,661:
type == "string"
and test("^(?<cc>[A-Z][A-Z])(?<sc>[0-9A-Z]{9})(?<cs>[0-9])$")
and (decodeBase36 | luhntest);</
'''The Task'''
<
"US0378331005",
"US0373831005",
Line 1,673:
| . + " => " + (if is_ISIN then "valid" else "invalid" end);
task</
{{out}}
<pre>
Line 1,687:
=={{header|Julia}}==
<
luhntest(x) = luhntest(parse(Int, x))
Line 1,699:
"US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"]
@printf("%-15s %5s\n", inum, ifelse(checkISIN(inum), "pass", "fail"))
end</
{{out}}
Line 1,712:
=={{header|Kotlin}}==
As the Luhn test method is only a few lines, it's reproduced here for convenience:
<
object Isin {
Line 1,748:
println("$isin\t -> ${if (Isin.isValid(isin)) "valid" else "not valid"}")
}
}</
{{out}}
Line 1,765:
{{works with|langur|0.8.10}}
<
val .t = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
val .numbers = s2n .s
Line 1,794:
write .key, ": ", .pass
writeln if(.pass == .tests[.key]: ""; " (ISIN TEST FAILED)")
}</
{{out}}
Line 1,806:
=={{header|Lua}}==
<
local revStr, s1, s2, digit, mod = n:reverse(), 0, 0
for pos = 1, #revStr do
Line 1,842:
"FR0000988040"
}
for _, ISIN in pairs(testCases) do print(ISIN, checkISIN(ISIN)) end</
{{out}}
<pre>US0378331005 true
Line 1,853:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
LuhnQ[n_Integer] := Block[{digits = Reverse@IntegerDigits@n}, Mod[Total[{digits[[;; ;; 2]], IntegerDigits[2 #] & /@ digits[[2 ;; ;; 2]]}, -1], 10] == 0]
VakudISINQ[sin_String] := Module[{s = ToUpperCase[sin]},
Line 1,868:
]
]
VakudISINQ /@ {"US0378331005", "US0373831005", "U50378331005", "US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"}</
{{out}}
<pre>{True, False, False, False, True, True, True}</pre>
=={{header|Nim}}==
<
const
Line 1,918:
echo &"{isin} is valid."
except ISINError:
echo &"{isin} is not valid: {getCurrentExceptionMsg()}."</
{{out}}
Line 1,931:
=={{header|Perl}}==
We reuse the <tt>luhn_test()</tt> function from ''[[Luhn test of credit card numbers#Perl]]''.
<
use English;
use POSIX;
Line 1,951:
split(//s, $isin));
return luhn_test($base10);
}</
{{out}}
<pre>1..7
Line 1,964:
=={{header|Phix}}==
Note this (slightly better) version of Luhn() has the reverse() inside it, whereas the original did not.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Luhn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">st</span><span style="color: #0000FF;">)</span>
Line 2,007:
<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;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">reasons</span><span style="color: #0000FF;">[</span><span style="color: #000000;">valid_ISIN</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: #000000;">1</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 2,021:
=={{header|PicoLisp}}==
Using the <tt>luhn</tt> function defined at ''[[Luhn test of credit card numbers#PicoLisp]]'':
<
(let Str (mapcar char (chop Str))
(and
Line 2,043:
"AU0000XVGZA3"
"AU0000VXGZA3"
"FR0000988040" ) ) )</
{{out}}
<pre>(0 NIL NIL NIL 0 0 0)</pre>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function Test-ISIN
{
Line 2,112:
(10 - ($sum % 10)) % 10 -match $checkDigit
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
"US0378331005","US0373831005","US0337833103","AU0000XVGZA3","AU0000VXGZA3","FR0000988040" | ForEach-Object {
[PSCustomObject]@{
Line 2,120:
}
}
</syntaxhighlight>
{{Out}}
<pre>
Line 2,134:
=={{header|PureBasic}}==
<
Procedure.b Check_ISIN(*c.Character)
Line 2,182:
CloseFile(0)
EndIf
Input()</
{{Out}}
<pre>US0378331005 TRUE
Line 2,194:
=={{header|Python}}==
<
if len(a) != 12 or not all(c.isalpha() for c in a[:2]) or not all(c.isalnum() for c in a[2:]):
return False
Line 2,226:
"AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"]]
# [True, False, False, False, True, True, True]</
=={{header|Quackery}}==
Line 2,232:
<code>luhn</code> is defined at [[Luhn test of credit card numbers#Quackery]].
<
char A char z 1+ within
swap
Line 2,263:
$ "AU0000VXGZA3" task
$ "FR0000988040" task
</syntaxhighlight>
{{out}}
Line 2,277:
=={{header|Racket}}==
<
#lang racket
Line 2,311:
(map isin-test? test-cases)
;; -> '(#t #f #f #f #t #t #t)
</syntaxhighlight>
{{out}}
Line 2,323:
Using the <tt>luhn-test</tt> function from the ''[[Luhn test of credit card numbers#Raku|Luhn test of credit card numbers]]'' task.
<syntaxhighlight lang="raku"
^ <[A..Z]>**2 <[A..Z0..9]>**9 <[0..9]> $
<?{ luhn-test $/.comb.map({ :36($_) }).join }>
Line 2,345:
AU0000VXGZA3
FR0000988040
>;</
{{out}}
Line 2,359:
=={{header|REXX}}==
<
parse arg z /*obtain optional ISINs from the C.L.*/
if z='' then z= "US0378331005 US0373831005 U50378331005 US03378331005 AU0000XVGZA3" ,
Line 2,385:
$= $ + substr(y, j, 1) + left(_, 1) + substr(_, 2 , 1, 0)
end /*j*/ /* [↑] sum the odd and even digits.*/
return right($, 1)==0 /*return "1" if number passed Luhn test*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,398:
=={{header|Ring}}==
<
# Project : Validate International Securities Identification Number
Line 2,479:
next
return sumarr
</syntaxhighlight>
Output:
<pre>
Line 2,493:
=={{header|Ruby}}==
Using a pre-existing luhn method:
<
def valid_isin?(str)
Line 2,508:
FR0000988040).map{|tc| valid_isin?(tc) }
# => [true, false, false, false, true, true, true]</
=={{header|Rust}}==
<
use luhn_cc::compute_luhn;
Line 2,556:
return compute_luhn(number);
}
</syntaxhighlight>
=={{header|SAS}}==
<
length isin $20 ok $1;
input isin;
Line 2,606:
FR0000988040
;
run;</
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/D9ax4Js/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/yOymYqoPSEeA7K7rjgn65g Scastie (remote JVM)].
<
val isins = Seq("US0378331005", "US0373831005", "U50378331005",
"US03378331005", "AU0000XVGZA3","AU0000VXGZA3", "FR0000988040")
Line 2,649:
isins.foreach(isin => println(f"$isin is ${if (ISINtest(isin)) "" else "not"}%s valid"))
}</
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 2,707:
RETURN RET;
END @
</syntaxhighlight>
Output:
<pre>
Line 2,773:
=={{header|Tcl}}==
<syntaxhighlight lang
A proc like assert is always good to have around. This one tries to report values used in its expression using subst:
<
if {![uplevel 1 [list expr $expr]]} {
set msg "{$expr}"
Line 2,781:
tailcall throw {ASSERT ERROR} $msg
}
}</
isin itself is a simple package. We compute the alphabet when the package is loaded in _init, because that's more fun than typing out the table:
<
proc _init {} { ;# sets up the map used on every call
variable map
Line 2,819:
}
}</
To run the test suite, we use the tcltest framework included with Tcl:
<
tcltest::test isin-1 "Test isin validation" -body {
Line 2,840:
}
return ok
} -result ok</
=={{header|Transact-SQL}}==
<syntaxhighlight lang="transact-sql">
CREATE FUNCTION dbo._ISINCheck( @strISIN VarChar(40) )
RETURNS bit
Line 2,886:
RETURN @bValid
END
</syntaxhighlight>
Testing
<syntaxhighlight lang="transact-sql">
-- Testing. The following tests all pass.
;WITH ISIN_Tests AS
Line 2,902:
)
SELECT ISIN, Expected, dbo._ISINCheck(ISIN) AS TestResult FROM ISIN_Tests ORDER BY ISIN
</syntaxhighlight>
=={{header|VBScript}}==
<
buf=buf&test("US0378331005")&vbCrLf
Line 2,951:
end if
test=cc&" "&msg
end function 'test </
{{out}}
<pre>
Line 2,966:
{{works with|Visual Basic|VB6 Standard}}
Calls LuhnCheckPassed() function described at [[Luhn_test_of_credit_card_numbers#Visual_Basic]]
<
Dim s As String, c As String
Dim i As Long
Line 2,985:
IsValidISIN = LuhnCheckPassed(s)
End If
End Function</
Test:
<
Debug.Assert IsValidISIN("US0378331005")
Debug.Assert Not IsValidISIN("US0373831005")
Line 2,996:
Debug.Assert IsValidISIN("FR0000988040")
Debug.Assert Not IsValidISIN("FR000098804O")
End Sub</
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Imports System.Text.RegularExpressions
Line 3,048:
End Sub
End Module</
{{out}}
<pre>US0378331005 is valid
Line 3,060:
=={{header|Vlang}}==
{{trans|go}}
<
const (
Line 3,114:
}
}
}</
{{out}}
<pre>expected true for US0378331005, got false
Line 3,127:
{{libheader|Wren-fmt}}
The Luhn test method is reproduced here for convenience.
<
import "/trait" for Stepped
import "/fmt" for Conv, Fmt
Line 3,166:
var ans = (isin.call(test)) ? "valid" : "not valid"
System.print("%(Fmt.s(-13, test)) -> %(ans)")
}</
{{out}}
Line 3,180:
=={{header|XPL0}}==
<
func Luhn(Str); \Return 'true' if digits in Str pass Luhn test
Line 3,232:
CrLf(0);
];
]</
{{out}}
Line 3,247:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
local i, j, s1, s2, l
Line 3,301:
if luhntest(test_str$) then print " Valid" else print " Invalid, checksum error" end if
loop
</syntaxhighlight>
=={{header|zkl}}==
Uses the luhn test from [[Luhn_test_of_credit_card_numbers#zkl]] (copied here as it is short).
<
RegExp(String("^","[A-Z]"*2,"[A-Z0-9]"*9,"[0-9]$")).matches(isin) and
luhnTest(isin.split("").apply("toInt",36).concat().toInt())
Line 3,312:
0 == (n.split().reverse().reduce(fcn(s,n,clk){
s + if(clk.inc()%2) n else 2*n%10 + n/5 },0,Ref(1)) %10)
}</
<
foreach isin in (T("US0378331005","US0373831005","U50378331005",
"US03378331005","AU0000XVGZA3","AU0000VXGZA3","FR0000988040")){
println(isin," --> ",validateISIN(isin));
}</
{{out}}
<pre>
|