Validate International Securities Identification Number: Difference between revisions
Content added Content deleted
(Added Wren) |
(Added XPL0 example.) |
||
Line 2,887: | Line 2,887: | ||
AU0000VXGZA3 -> valid |
AU0000VXGZA3 -> valid |
||
FR0000988040 -> valid |
FR0000988040 -> valid |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<lang XPL0>string 0; \use zero-terminated strings |
|||
func Luhn(Str); \Return 'true' if digits in Str pass Luhn test |
|||
char Str; |
|||
int Len, Sum, I, Dig; |
|||
[Len:= 0; \find length of Str |
|||
while Str(Len) do Len:= Len+1; |
|||
Sum:= 0; \sum even and odd digits |
|||
for I:= 0 to Len-1 do \(no need to reverse) |
|||
[if (I xor Len) & 1 then |
|||
Sum:= Sum + Str(I) - ^0 |
|||
else [Dig:= Str(I) - ^0; |
|||
Dig:= Dig*2; |
|||
Sum:= Sum + Dig/10 + rem(0); |
|||
]; |
|||
]; |
|||
return rem(Sum/10) = 0; |
|||
]; \Luhn |
|||
func Valid(Str); \Return 'true' if valid ISIN code |
|||
char Str, Str2(100); |
|||
int Sum, I, J, C, V; |
|||
[J:= 0; |
|||
for I:= 0 to 12-1 do \convert letters in Str to digits in Str2 |
|||
[C:= Str(I); |
|||
case of |
|||
C>=^0 & C<=^9: [Str2(J):= C; J:= J+1]; |
|||
C>=^A & C<=^Z: [Str2(J):= (C-^A+10)/10 + ^0; J:= J+1; |
|||
Str2(J):= rem(0) + ^0; J:= J+1] |
|||
other return false; |
|||
if I=1 & J#4 then return false; \first two chars not letters |
|||
]; |
|||
if Str(I) # 0 then return false; \too long |
|||
Str2(J):= 0; \terminate string |
|||
return Luhn(Str2); |
|||
]; \Valid |
|||
int ISIN, N; |
|||
[ISIN:= ["US0378331005", |
|||
"US0373831005", |
|||
"U50378331005", |
|||
"US03378331005", |
|||
"AU0000XVGZA3", |
|||
"AU0000VXGZA3", |
|||
"FR0000988040"]; |
|||
for N:= 0 to 7-1 do |
|||
[Text(0, ISIN(N)); |
|||
Text(0, if Valid(ISIN(N)) |
|||
then " is valid" |
|||
else " is not valid"); |
|||
CrLf(0); |
|||
]; |
|||
]</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> |
</pre> |
||