How to resolve the algorithm Validate International Securities Identification Number step by step in the C# programming language
How to resolve the algorithm Validate International Securities Identification Number step by step in the C# 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 C# programming language
The provided C# code defines two classes: IsinValidator
and Program
. It is designed to validate International Securities Identification Numbers (ISINs). Let's break down the code in detail:
IsinValidator
Class:
- The
IsinValidator
class contains static methods for validating ISINs. IsValidIsin(string isin)
: This is the main method that returns a boolean indicating whether the input string is a valid ISIN. It combines two tests:IsinRegex.IsMatch(isin)
: This checks if the input string matches the regular expression pattern defined byIsinRegex
.LuhnTest(Digitize(isin))
: This checks if the digits extracted from the ISIN pass the Luhn algorithm test, which is a checksum used to validate financial numbers.
IsinRegex
: A staticRegex
object defining the regular expression pattern for valid ISINs. It matches strings that start with two uppercase letters, followed by nine uppercase letters or digits, and ending with a single digit.Digitize(string isin)
: This method converts the input ISIN string into a string of digits by mapping each character to its numeric value. Characters 'A' through 'Z' are mapped to 10-35. Digits '0' through '9' are mapped to their corresponding decimal values.LuhnTest(string number)
: This method performs the Luhn algorithm on the input string of digits. It reverses the string, converts each character to an integer, calculates a summand for each digit, and sums the results. If the result is divisible by 10, the test passes.Summand(int digit, int i)
: This method calculates a summand for a given digit based on its position. The formula is digit + (position modulo 2) * (digit - digit divided by 5 multiplied by 9).DigitValue(char c)
: This method maps a character to its numeric value. Characters 'A' through 'Z' are mapped to 10-35. Digits '0' through '9' are mapped to their corresponding decimal values.
Program
Class:
- The
Program
class contains aMain
method that serves as the entry point of the program. - In the
Main
method:- An array of ISINs (
isins
) is defined. - For each ISIN in the array, it calls
IsinValidator.IsValidIsin
to determine if the ISIN is valid. - It prints whether each ISIN is valid or not valid to the console.
- An array of ISINs (
Sample Input and Output:
The example provided in the code includes an array of ISINs. The output for this input should look something like this:
US0378331005 is valid
US0373831005 is not valid
U50378331005 is not valid
US03378331005 is not valid
AU0000XVGZA3 is valid
AU0000VXGZA3 is not valid
FR0000988040 is valid
Source code in the csharp programming language
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace ValidateIsin
{
public static class IsinValidator
{
public static bool IsValidIsin(string isin) =>
IsinRegex.IsMatch(isin) && LuhnTest(Digitize(isin));
private static readonly Regex IsinRegex =
new Regex("^[A-Z]{2}[A-Z0-9]{9}\\d$", RegexOptions.Compiled);
private static string Digitize(string isin) =>
string.Join("", isin.Select(c => $"{DigitValue(c)}"));
private static bool LuhnTest(string number) =>
number.Reverse().Select(DigitValue).Select(Summand).Sum() % 10 == 0;
private static int Summand(int digit, int i) =>
digit + (i % 2) * (digit - digit / 5 * 9);
private static int DigitValue(char c) =>
c >= '0' && c <= '9'
? c - '0'
: c - 'A' + 10;
}
public class Program
{
public static void Main()
{
string[] isins =
{
"US0378331005",
"US0373831005",
"U50378331005",
"US03378331005",
"AU0000XVGZA3",
"AU0000VXGZA3",
"FR0000988040"
};
foreach (string isin in isins) {
string validOrNot = IsinValidator.IsValidIsin(isin) ? "valid" : "not valid";
Console.WriteLine($"{isin} is {validOrNot}");
}
}
}
}
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Pascal programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Objeck programming language
You may also check:How to resolve the algorithm Boustrophedon transform step by step in the Julia programming language
You may also check:How to resolve the algorithm Penney's game step by step in the VBA programming language