64 lines
3.6 KiB
Text
64 lines
3.6 KiB
Text
An [[wp:International_Securities_Identification_Number|International Securities Identification Number]] (ISIN) is a unique international identifier for a financial security such as a stock or bond.
|
|
|
|
|
|
;Task:
|
|
Write a function or program that takes a string as input, and checks whether it is a valid ISIN.
|
|
|
|
It is only valid if it has the correct format, ''and'' the embedded checksum is correct.
|
|
|
|
Demonstrate that your code passes the test-cases listed below.
|
|
|
|
|
|
;Details:
|
|
The format of an ISIN is as follows:
|
|
|
|
<!-- BEGIN DIAGRAM -->
|
|
<div style="margin:3em; white-space:nowrap; line-height:20px">
|
|
<div><span style="font-size:20px; font-family:'Lucida Console',Monaco,monospace"><span style="color:green; margin:0 0 0 10px">┌───────────── </span></span><span style="color:green">a 2-character ISO country code (A-Z)</span></div>
|
|
<div><span style="font-size:20px; font-family:'Lucida Console',Monaco,monospace"><span style="color:green; margin:0 -10px 0 10px">│</span> <span style="color:blue; margin:0 0 0 10px">┌─────────── </span></span><span style="color:blue">a 9-character security code (A-Z, 0-9)</span></div>
|
|
<div><span style="font-size:20px; font-family:'Lucida Console',Monaco,monospace"><span style="color:green; margin:0 -10px 0 10px">│</span> <span style="color:blue; margin:0 -5px 0 10px">│</span> <span style="color:red; margin:0 0 0 5px">┌── </span></span><span style="color:red">a checksum digit (0-9)</span></div>
|
|
<div style="font-size:20px; font-family:'Lucida Console',Monaco,monospace"><span style="background: #d9ebd9; color:green; border:solid 1px green; margin:0 1px">AU</span><span style="background:#e0e0ff; color:blue; border:solid 1px blue; margin:0 1px">0000XVGZA</span><span style="background:#feefef; color:red; border:solid 1px red; margin:0 1px">3</span></div>
|
|
</div>
|
|
<!-- END DIAGRAM -->
|
|
|
|
|
|
For this task, you may assume that any 2-character alphabetic sequence is a valid country code.
|
|
|
|
The checksum can be validated as follows:
|
|
# '''Replace letters with digits''', by converting each character from base 36 to base 10, e.g. <code>AU0000XVGZA3</code> →<code>1030000033311635103</code>.
|
|
# '''Perform the Luhn test on this base-10 number.'''<br>There is a separate task for this test: ''[[Luhn test of credit card numbers]]''.<br>You don't have to replicate the implementation of this test here ─── you can just call the existing function from that task. (Add a comment stating if you did this.)
|
|
|
|
|
|
;Test cases:
|
|
:::: {| class="wikitable"
|
|
! ISIN
|
|
! Validity
|
|
! Comment
|
|
|-
|
|
| <tt>US0378331005</tt> || valid ||
|
|
|-
|
|
| <tt>US0373831005</tt> || not valid || The transposition typo is caught by the checksum constraint.
|
|
|-
|
|
| <tt>U50378331005</tt> || not valid || The substitution typo is caught by the format constraint.
|
|
|-
|
|
| <tt>US03378331005</tt> || not valid || The duplication typo is caught by the format constraint.
|
|
|-
|
|
| <tt>AU0000XVGZA3</tt> || valid ||
|
|
|-
|
|
| <tt>AU0000VXGZA3</tt> || valid || Unfortunately, not ''all'' transposition typos are caught by the checksum constraint.
|
|
|-
|
|
| <tt>FR0000988040</tt> || valid ||
|
|
|}
|
|
|
|
(The comments are just informational. Your function should simply return a Boolean result. See [[#Raku]] for a reference solution.)
|
|
|
|
|
|
Related task:
|
|
* [[Luhn test of credit card numbers]]
|
|
|
|
|
|
;Also see:
|
|
* [https://www.isincodes.net/validate-isin/ Interactive online ISIN validator]
|
|
* Wikipedia article: [[wp:International_Securities_Identification_Number|International Securities Identification Number]]
|
|
<br><br>
|
|
|