Validate International Securities Identification Number: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(One final tweak to 'Summand')
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 474:
 
/* will print: T F F F T T T */</lang>
 
=={{header|C sharp|C#}}==
{
<lang csharp>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}");
}
}
}
}</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>
 
=={{header|C++}}==
Line 538 ⟶ 600:
}
</lang>
=={{header|C#|C sharp}}==
{
<lang csharp>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}");
}
}
}
}</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>
 
=={{header|Caché ObjectScript}}==
Line 1,712 ⟶ 1,713:
ok 6 - Test 6
ok 7 - Test 7</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.12}}
 
Using the <tt>luhn-test</tt> function from the ''[[Luhn test of credit card numbers#Perl 6|Luhn test of credit card numbers]]'' task.
 
<lang perl6>my $ISIN = /
^ <[A..Z]>**2 <[A..Z0..9]>**9 <[0..9]> $
<?{ luhn-test $/.comb.map({ :36($_) }).join }>
/;
 
sub luhn-test ($number --> Bool) {
my @digits = $number.comb.reverse;
my $sum = @digits[0,2...*].sum
+ @digits[1,3...*].map({ |($_ * 2).comb }).sum;
return $sum %% 10;
}
 
# Testing:
 
say "$_ is { m/$ISIN/ ?? "valid" !! "not valid"}" for <
US0378331005
US0373831005
U50378331005
US03378331005
AU0000XVGZA3
AU0000VXGZA3
FR0000988040
>;</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>
 
=={{header|Phix}}==
Line 2,058 ⟶ 2,019:
 
'(#t #f #f #f #t #t #t)
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.12}}
 
Using the <tt>luhn-test</tt> function from the ''[[Luhn test of credit card numbers#Perl 6|Luhn test of credit card numbers]]'' task.
 
<lang perl6>my $ISIN = /
^ <[A..Z]>**2 <[A..Z0..9]>**9 <[0..9]> $
<?{ luhn-test $/.comb.map({ :36($_) }).join }>
/;
 
sub luhn-test ($number --> Bool) {
my @digits = $number.comb.reverse;
my $sum = @digits[0,2...*].sum
+ @digits[1,3...*].map({ |($_ * 2).comb }).sum;
return $sum %% 10;
}
 
# Testing:
 
say "$_ is { m/$ISIN/ ?? "valid" !! "not valid"}" for <
US0378331005
US0373831005
U50378331005
US03378331005
AU0000XVGZA3
AU0000VXGZA3
FR0000988040
>;</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>
 
=={{header|REXX}}==
10,327

edits