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

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Validate International Securities Identification Number step by step in the D 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 D programming language

Source code in the d programming language

import std.stdio;

void main() {
    auto isins = [
        "US0378331005",
        "US0373831005",
        "U50378331005",
        "US03378331005",
        "AU0000XVGZA3",
        "AU0000VXGZA3",
        "FR0000988040",
    ];
    foreach (isin; isins) {
        writeln(isin, " is ", ISINvalidate(isin) ? "valid" : "not valid");
    }
}

bool ISINvalidate(string isin) {
    import std.array : appender;
    import std.conv : to;
    import std.regex : matchFirst;
    import std.string : strip, toUpper;

    isin = isin.strip.toUpper;

    if (isin.matchFirst(`^[A-Z]{2}[A-Z0-9]{9}\d$`).empty) {
        return false;
    }

    auto sb = appender!string;
    foreach (c; isin[0..12]) {
        sb.put(
            [c].to!int(36)
               .to!string
        );
    }

    import luhn;
    return luhnTest(sb.data);
}


  

You may also check:How to resolve the algorithm String append step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Unprimeable numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Clean programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Prolog programming language
You may also check:How to resolve the algorithm Program name step by step in the Gambas programming language