Validate International Securities Identification Number: Difference between revisions

added Factor
(→‎{{header|Java}}: Fixed this example so the input and output now correspond. Embedded the luhnTest method in the ISIN class.)
(added Factor)
Line 713:
AU0000VXGZA3 true
FR0000988040 true
</pre>
 
=={{header|Factor}}==
We re-use the <code>luhn?</code> word from ''[[Luhn test of credit card numbers#Factor]]''.
<lang factor>USING: combinators.short-circuit.smart formatting kernel luhn
math math.parser qw sequences strings unicode ;
IN: rosetta-code.isin
 
CONSTANT: test-cases qw{
US0378331005 US0373831005 U50378331005 US03378331005
AU0000XVGZA3 AU0000VXGZA3 FR0000988040
}
 
: valid-length? ( str -- ? ) length 12 = ;
 
: valid-country-code? ( str -- ? ) first2 [ Letter? ] both? ;
 
: valid-security-code? ( str -- ? )
[ 2 11 ] dip subseq [ alpha? ] all? ;
: valid-checksum-digit? ( str -- ? ) last digit? ;
: valid-format? ( str -- ? ) {
[ valid-length? ]
[ valid-country-code? ]
[ valid-security-code? ]
[ valid-checksum-digit? ]
} && ;
: string>base36 ( str -- n )
>upper [ dup LETTER? [ 55 - number>string ] [ 1string ] if ]
{ } map-as concat string>number ;
: isin? ( str -- ? )
{ [ valid-format? ] [ string>base36 luhn? ] } && ;
: main ( -- )
test-cases [
dup isin? "" " not" ? "%s is%s valid\n" printf
] each ;
MAIN: main</lang>
{{out}}
<pre>
US0378331005 is valid
US0373831005 is not valid
U50378331005 is not valid
US03378331005 is not valid
AU0000XVGZA3 is valid
AU0000VXGZA3 is valid
FR0000988040 is valid
</pre>
 
1,827

edits