How to resolve the algorithm Validate International Securities Identification Number step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Validate International Securities Identification Number step by step in the AppleScript programming language

Table of Contents

Problem Statement

An International Securities Identification Number (ISIN) is a unique international identifier for a financial security such as a stock or bond.

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.

The format of an ISIN is as follows:

For this task, you may assume that any 2-character alphabetic sequence is a valid country code. The checksum can be validated as follows:

(The comments are just informational.   Your function should simply return a Boolean result.   See #Raku for a reference solution.)

Related task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Validate International Securities Identification Number step by step in the AppleScript programming language

Source code in the applescript programming language

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

on ISINTest(ISIN)
    -- Check that the input is both text and 12 characters long …
    if not ((ISIN's class is text) and ((count ISIN) is 12)) then return false
    -- … and that it has the required format.
    set ISIN to current application's class "NSMutableString"'s stringWithString:(ISIN)
    if ((ISIN's rangeOfString:("^[A-Z]{2}[0-9A-Z]{9}[0-9]$") options:(current application's NSRegularExpressionSearch) range:({0, ISIN's |length|()}))'s |length|() is 0) then return false
    -- Replace all letters with text representations of equivalent decimal numbers in the range 10 to 35.
    set letterCharacters to characters of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    repeat with i from 1 to 26
        tell ISIN to replaceOccurrencesOfString:(item i of letterCharacters) withString:((i + 9) as text) options:(0) range:({0, its |length|()})
    end repeat
    
    -- Apply the Luhn test handler from the "Luhn test of credit card numbers" task.
    -- <https://www.rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Straightforward>
    return luhnTest(ISIN as text)
end ISINTest

-- Test code:
set testResults to {}
repeat with ISIN in {"US0378331005", "US0373831005", "U50378331005", "US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"}
    set end of testResults to {testNumber:ISIN's contents, valid:ISINTest(ISIN)}
end repeat
return testResults


{{testNumber:"US0378331005", valid:true}, {testNumber:"US0373831005", valid:false}, {testNumber:"U50378331005", valid:false}, {testNumber:"US03378331005", valid:false}, {testNumber:"AU0000XVGZA3", valid:true}, {testNumber:"AU0000VXGZA3", valid:true}, {testNumber:"FR0000988040", valid:true}}


  

You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the F# programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Ada programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Character codes step by step in the Axe programming language