Validate International Securities Identification Number: Difference between revisions

(Add bruijn)
 
(10 intermediate revisions by 4 users not shown)
Line 2,102:
 
=={{header|langur}}==
The luhn test is repeated here for simplicity (from Luhn_test_of_credit_card_numbers#langur).
 
<syntaxhighlight lang="langur">val .luhntest = f(.s) {
val luhntest = fn(s) {
val .t = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
val .numberst = s2n[0, .s2, 4, 6, 8, 1, 3, 5, 7, 9]
val .oddevennumbers = len(.numbers)s rem-> 2s2n
val oddeven = len(numbers) rem 2
 
for[=0] .i of .numbers {
_for += if(. i rem 2 == .oddeven: .numbers[.i]; .t[.numbers[.i]+1]){
numbers[i]
} else {
t[numbers[i]+1]
}
} div 10
}
 
val .isintest = ffn(.s) {
matching(s -> re/^[A-Z][A-Z][0-9A-Z]{9}[0-9]$/, .s) and
.luhntest(joins -> s2n .s)-> join -> luhntest
}
 
val .tests = h{
"US0378331005": true,
"US0373831005": false,
Line 2,129 ⟶ 2,134:
}
 
for .key in sort(keys .(tests)) {
val .pass = .isintest(.key)
write .key, ": ", .pass
writeln if(.pass == .tests[.key]: ""; " (ISIN TEST FAILED)")
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,698 ⟶ 2,704:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program validates the checksum digit for an International Securities ID number.*/
parse/*REXX argprogram zvalidates International Securities ID numbers. /*obtain optional ISINs from the C.L.*/
Parse Arg z /*obtain ISINs from the C.L. */
if z='' then z= "US0378331005 US0373831005 U50378331005 US03378331005 AU0000XVGZA3" ,
If z='' Then 'AU0000VXGZA3 FR0000988040' /* [?] use the default list of ISINs. */
z='US0378331005 US0373831005 U50378331005 US03378331005',
/* [↓] process all specified ISINs.*/
'US037*331005',
do n=1 for words(z); x=word(z, n); y= x /*obtain an ISIN from the Z list. */
'XY037833100Z AU0000XVGZA3 AU0000VXGZA3 FR0000988040'
$= /* [↓] construct list of ISIN digits. */
valid='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*X must contain 0-->9 A-->Z */
do k=1 for length(x); _= substr(x,k,1) /*the ISIN may contain alphabetic chars*/
Do n=1 To words(z) /* [?] process all specified ISINs.*/
p= pos(_, 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) /*X must contain A──►Z, 0──►9.*/
x=word(z,n) if p==0 then y= /*obtain an ISIN from the Z list. /*trigger "not" valid below.*/
p=verify(x,valid,'N')
else $= $ || p-1 /*convert X string (base 36 ──► dec).*/
If p>0 Then msg='invalid character in position' p':' substr(x,p,1)
end /*k*/ /* [↑] convert alphabetic ──► digits.*/
Else Do
@= /*placeholder for the "not" in message.*/
dd='' if length(y)\==12 then @= "not" /*see if[?] the ISINconstruct islist exactlyof 12ISIN charsdigits. */
Do k=1 To length(x)
if \datatype( left(x,2),'U') then @= "not" /* " " " " 1st 2 chars cap. let.*/
if \datatype(right_=substr(x,k,1),'W') then @= "not" /* " " " " last char not a digit /*the ISIN may contain alphabetic chars*/
if @ p==''pos(_,valid) then if \luhn($) then @= "not" /*X contains 0-->9 A-->Z " " " " passed the Luhn test.*/
say right(x,dd=dd||p-1 30) right(@, 5) "valid" /*display the yea or nay message /*convert X string (base 36 --? dec).*/
End
end /*n*/ /* [↑] 1st 3 IFs could've been combined*/
msg=''
exit /*stick a fork in it, we're all done. */
Select
/*──────────────────────────────────────────────────────────────────────────────────────*/
When length(x)\==12 Then msg='not exactly 12 chars'
Luhn: procedure; parse arg x; $= 0 /*get credit card number; zero $ sum. */
y=When reverse\datatype( left(0x, length(x) // 2)x,'U') Then msg='not starting with 2 /*add leading zero if needed, &capital reverse*/chars'
When \datatype(right(x,1),'W') Then msg='last character is not a digit'
do j=1 to length(y)-1 by 2; _= 2 * substr(y, j+1, 1)
Otherwise
$= $ + substr(y, j, 1) + left(_, 1) + substr(_, 2 , 1, 0)
If \luhn(dd) Then msg='does endnot /*j*/ /* [↑] sumpass the Luhn odd and even digits.*/test'
End
return right($, 1)==0 /*return "1" if number passed Luhn test*/</syntaxhighlight>
End
If msg='' Then
Say right(x,15) ' valid' /* display the positive message. */
Else
Say right(x,15) 'not valid:' msg /* display the problem */
End /*n*/
Exit /*stick a fork in it, we're all done */
/*-----------------------------------------------------------------------------------*/
luhn: Procedure
Parse Arg x /* get credit card number; */
dsum=0 /* zero digit sum. */
y=reverse(left(0,length(x)//2)x) /* add leading zero If needed & reverse */
Do j=1 To length(y)-1 By 2
_=2*substr(y,j+1,1)
dsum=dsum+substr(y,j,1)+left(_,1)+substr(_,2,1,0)
End
Return right(dsum,1)==0 /* Return 1 if number passed Luhn test */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
US0378331005 valid
US0373831005 not valid: does not pass the Luhn test
US0373831005 not valid
U50378331005 not valid: not starting with 2 capital chars
U50378331005 not valid
US03378331005 not valid: not exactly 12 chars
US03378331005 not valid
US037*331005 not valid: invalid character in position 6: *
AU0000XVGZA3 valid
XY037833100Z not valid: last character is not a digit
AU0000VXGZA3 valid
FR0000988040 AU0000XVGZA3 valid
AU0000VXGZA3 valid
FR0000988040 valid
</pre>
 
Line 2,828 ⟶ 2,853:
AU0000VXGZA3 -> Valid
FR0000988040 -> Valid
</pre>
 
=={{header|RPL}}==
<code>LUHN?</code> is defined at [[Luhn test of credit card numbers#RPL|Luhn test of credit card numbers]]
{{works with|RPL|HP48-R}}
« '''IF''' DUP SIZE 12 ≠ '''THEN''' DROP 0
'''ELSE'''
""
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB
'''IF''' "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" SWAP POS '''THEN''' LASTARG 1 - + '''END'''
'''NEXT'''
<span style="color:blue">LUHN?</span>
{ "AD" "AT" "AU" "BE" "CA" "DE" "ES" "FR" "GB" "HK" "IT" "US" "ZW" } <span style="color:grey">@ country codes sample </span>
ROT 1 2 SUB POS AND
'''END'''
» '<span style="color:blue">ISIN?</span>' STO
 
{"US0378331005" "US0373831005" "U50378331005" "US03378331005" "AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"}
1 « <span style="color:blue">ISIN?</span> » DOLIST
{{out}}
<pre>
1: { 1 0 0 0 1 1 1 }
</pre>
 
Line 3,243 ⟶ 3,291:
</syntaxhighlight>
 
=={{header|Uiua}}==
<syntaxhighlight lang="uiua">
Base ← "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Luhn ← =0◿10+⊃(/+⊢|/+∵(⍥(-9)>9.×2)⊡1)⍉⬚0↯∞_2⇌
Checksum ← Luhn≡⋕/◇⊂≡(□°⋕⊗)⊙(¤Base)
Format ← ××⊃(/×≡(≥@A)↙2|=12⧻|/↧∊:Base)
Valid ← ×⊃(Format|Checksum)
 
Tests ← {
"AU0000XVGZA3"
"US0378331005"
"US0373831005" # twiddled
"U50378331005" # 5 rather than S
"US03378331005" # Extra char
"AU0000XVGZA3"
"AU0000VXGZA3"
"FR0000988040"
"F00Ö00988040" # Illegal char
}
 
≡◇Valid Tests
</syntaxhighlight>
{{out}}
<pre>
[1 1 0 0 0 1 1 1 0]
</pre>
=={{header|VBScript}}==
<syntaxhighlight lang="vb">' Validate International Securities Identification Number - 03/03/2019
1,007

edits