Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
72
Task/IBAN/FreeBASIC/iban.freebasic
Normal file
72
Task/IBAN/FreeBASIC/iban.freebasic
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' List updated to release 72, 25 November 2016, of IBAN Registry (75 countries)
|
||||
Dim Shared countryCodes As String
|
||||
countryCodes = _
|
||||
"AD24 AE23 AL28 AT20 AZ28 BA20 BE16 BG22 BH22 BR29 BY28 CH21 CR22 CY28 CZ24 DE22 " _
|
||||
"DK18 DO28 EE20 ES24 FI18 FO18 FR27 GB22 GE22 GI23 GL18 GR27 GT28 HR21 HU28 IE22 " _
|
||||
"IL23 IQ23 IS26 IT27 JO30 KW30 KZ20 LB28 LC32 LI21 LT20 LU20 LV21 MC27 MD24 ME22 " _
|
||||
"MK19 MR27 MT31 MU30 NL18 NO15 PK24 PL28 PS29 PT25 QA29 RO24 RS22 SA24 SC31 SE24 " _
|
||||
"SI19 SK24 SM27 ST25 SV28 TL23 TN24 TR26 UA29 VG24 XK20"
|
||||
|
||||
Function checkCountryCode(cc As String) As Boolean
|
||||
Return Instr(countryCodes, cc)
|
||||
End Function
|
||||
|
||||
' To avoid having to use the GMP library, a piece-wise calculation is used
|
||||
Function mod97(s As String) As UInteger
|
||||
Dim r As UInteger = ValULng(Left(s, 9)) Mod 97
|
||||
Dim start As UInteger = 10
|
||||
While start < Len(s)
|
||||
r = ValULng(r & Mid(s, start, 7)) Mod 97
|
||||
start += 7
|
||||
Wend
|
||||
Return r
|
||||
End Function
|
||||
|
||||
Function validateIban(iban As Const String) As Boolean
|
||||
' remove spaces from IBAN
|
||||
Dim s As String = iban
|
||||
Dim count As Integer = 0
|
||||
For i As Integer = 0 To Len(s) - 1
|
||||
If s[i] = 32 Then
|
||||
For j As Integer = i + 1 To Len(s) - 1
|
||||
s[j - 1] = s[j]
|
||||
Next
|
||||
count += 1
|
||||
End If
|
||||
If i = Len(s) - 1 - count Then Exit For
|
||||
Next i
|
||||
If count > 0 Then
|
||||
s[Len(s) - count] = 0
|
||||
Dim p As UInteger Ptr = CPtr(UInteger Ptr, @s)
|
||||
*(p + 1) = Len(s) - count ''change length of string in descriptor
|
||||
End If
|
||||
|
||||
' check country code
|
||||
Dim isValid As Boolean = checkCountryCode(Left(s, 2) + Str(Len(s)))
|
||||
If Not isValid Then Return False
|
||||
|
||||
' move first 4 characters to end
|
||||
s = Mid(s, 5) + Left(s, 4)
|
||||
|
||||
' replace A to Z with numbers 10 To 35
|
||||
For i As Integer = Len(s) To 1 Step -1
|
||||
If s[i - 1] >= 65 AndAlso s[i - 1] <= 90 Then
|
||||
s = Left(s, i - 1) + Str(s[i - 1] - 55) + Mid(s, i + 1)
|
||||
End If
|
||||
Next
|
||||
|
||||
' do mod97 calculation
|
||||
Return mod97(s) = 1 '' remainder needs to be 1 for validity
|
||||
End Function
|
||||
|
||||
Dim As String ibans(1 To 2) = {"GB82 WEST 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32"}
|
||||
For i As Integer = 1 To 2
|
||||
Dim isValid As Boolean = validateIban(ibans(i))
|
||||
Print ibans(i); IIf(isValid, " : may be valid", " : is not valid")
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
34
Task/IBAN/Nim/iban.nim
Normal file
34
Task/IBAN/Nim/iban.nim
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import tables, strutils, re, bigints
|
||||
|
||||
let countryLen = toTable({
|
||||
"AL": 28, "AD": 24, "AT": 20, "AZ": 28, "BE": 16, "BH": 22, "BA": 20, "BR": 29,
|
||||
"BG": 22, "CR": 21, "HR": 21, "CY": 28, "CZ": 24, "DK": 18, "DO": 28, "EE": 20,
|
||||
"FO": 18, "FI": 18, "FR": 27, "GE": 22, "DE": 22, "GI": 23, "GR": 27, "GL": 18,
|
||||
"GT": 28, "HU": 28, "IS": 26, "IE": 22, "IL": 23, "IT": 27, "KZ": 20, "KW": 30,
|
||||
"LV": 21, "LB": 28, "LI": 21, "LT": 20, "LU": 20, "MK": 19, "MT": 31, "MR": 27,
|
||||
"MU": 30, "MC": 27, "MD": 24, "ME": 22, "NL": 18, "NO": 15, "PK": 24, "PS": 29,
|
||||
"PL": 28, "PT": 25, "RO": 24, "SM": 27, "SA": 24, "RS": 22, "SK": 24, "SI": 19,
|
||||
"ES": 24, "SE": 24, "CH": 21, "TN": 24, "TR": 26, "AE": 23, "GB": 22, "VG": 24})
|
||||
|
||||
proc validIban(iban: string): bool =
|
||||
# Ensure upper alphanumeric input
|
||||
var iban = iban.replace(" ","").replace("\t","")
|
||||
if not iban.match(re"^[\dA-Z]+$"):
|
||||
return false
|
||||
|
||||
# Validate country code against expected length
|
||||
if iban.len != countryLen[iban[0..1]]:
|
||||
return false
|
||||
|
||||
# Shift and convert
|
||||
iban = iban[4..iban.high] & iban[0..3]
|
||||
var digits = ""
|
||||
for ch in iban:
|
||||
case ch
|
||||
of '0'..'9': digits.add($(ch.ord - '0'.ord))
|
||||
of 'A'..'Z': digits.add($(ch.ord - 'A'.ord + 10))
|
||||
else: discard
|
||||
result = initBigInt(digits) mod 97 == 1
|
||||
|
||||
for account in ["GB82 WEST 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32"]:
|
||||
echo account, " validation is: ", validIban account
|
||||
31
Task/IBAN/Sidef/iban.sidef
Normal file
31
Task/IBAN/Sidef/iban.sidef
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
func valid_iban(iban) {
|
||||
static len = Hash.new(
|
||||
AD=>24, AE=>23, AL=>28, AO=>25, AT=>20, AZ=>28, BA=>20, BE=>16, BF=>27,
|
||||
BG=>22, BH=>22, BI=>16, BJ=>28, BR=>29, CG=>27, CH=>21, CI=>28, CM=>27,
|
||||
CR=>21, CV=>25, CY=>28, CZ=>24, DE=>22, DK=>18, DO=>28, DZ=>24, EE=>20,
|
||||
EG=>27, ES=>24, FI=>18, FO=>18, FR=>27, GA=>27, GB=>22, GE=>22, GI=>23,
|
||||
GL=>18, GR=>27, GT=>28, HR=>21, HU=>28, IE=>22, IL=>23, IR=>26, IS=>26,
|
||||
IT=>27, JO=>30, KW=>30, KZ=>20, LB=>28, LI=>21, LT=>20, LU=>20, LV=>21,
|
||||
MC=>27, MD=>24, ME=>22, MG=>27, MK=>19, ML=>28, MR=>27, MT=>31, MU=>30,
|
||||
MZ=>25, NL=>18, NO=>15, PK=>24, PL=>28, PS=>29, PT=>25, QA=>29, RO=>24,
|
||||
RS=>22, SA=>24, SE=>24, SI=>19, SK=>24, SM=>27, SN=>28, TN=>24, TR=>26,
|
||||
UA=>29, VG=>24,
|
||||
);
|
||||
|
||||
# Ensure upper alphanumeric input.
|
||||
iban -= /\s+/g;
|
||||
iban.uc! ~~ /^[0-9A-Z]+\z/ || return false;
|
||||
|
||||
# Validate country code against expected length.
|
||||
var cc = iban.substr(0, 2);
|
||||
iban.len == len{cc} || return false;
|
||||
|
||||
# Shift and convert.
|
||||
iban.sub!(/(.{4})(.+)/, {|a,b| b+a});
|
||||
iban.gsub!(/([A-Z])/, {|a| a.ord - 55});
|
||||
|
||||
iban.to_i % 97 == 1;
|
||||
}
|
||||
|
||||
say valid_iban("GB82 WEST 1234 5698 7654 32"); #=> true
|
||||
say valid_iban("GB82 TEST 1234 5698 7654 32"); #=> false
|
||||
8
Task/IBAN/jq/iban-1.jq
Normal file
8
Task/IBAN/jq/iban-1.jq
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# strip the input string of spaces and tabs:
|
||||
gsub("[ \t]";"")
|
||||
# check the string is ALPHAnumeric
|
||||
| test("^[A-Z0-9]+$")
|
||||
# check its length is as determined by the country code:
|
||||
and length == $lengths[.[0:2]]
|
||||
# check the mod 97 criterion:
|
||||
and ( (.[4:] + .[0:4]) | letters2digits | remainder) == 1
|
||||
38
Task/IBAN/jq/iban-2.jq
Normal file
38
Task/IBAN/jq/iban-2.jq
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
def letters2digits:
|
||||
65 as $A | 90 as $Z
|
||||
| ($A - 10) as $ten
|
||||
| explode
|
||||
| map( if $A <= . and . <= $Z
|
||||
then (. - $ten) | tostring
|
||||
else [.] | implode
|
||||
end )
|
||||
| join("");
|
||||
|
||||
# jq currently does not have unlimited-precision integer arithmetic
|
||||
# and so we define a special-purpose "mod 97" filter:
|
||||
# input: a string representing a decimal
|
||||
# output: its remainder modulo 97 as a number
|
||||
def remainder:
|
||||
if length < 15 then (.|tonumber) % 97
|
||||
else (.[0:14] | remainder | tostring) as $r1
|
||||
| ($r1 + .[14:]) | remainder
|
||||
end;
|
||||
|
||||
def is_valid_iban:
|
||||
{
|
||||
"AL": 28, "AD": 24, "AT": 20, "AZ": 28, "BE": 16, "BH": 22, "BA": 20, "BR": 29,
|
||||
"BG": 22, "CR": 21, "HR": 21, "CY": 28, "CZ": 24, "DK": 18, "DO": 28, "EE": 20,
|
||||
"FO": 18, "FI": 18, "FR": 27, "GE": 22, "DE": 22, "GI": 23, "GR": 27, "GL": 18,
|
||||
"GT": 28, "HU": 28, "IS": 26, "IE": 22, "IL": 23, "IT": 27, "KZ": 20, "KW": 30,
|
||||
"LV": 21, "LB": 28, "LI": 21, "LT": 20, "LU": 20, "MK": 19, "MT": 31, "MR": 27,
|
||||
"MU": 30, "MC": 27, "MD": 24, "ME": 22, "NL": 18, "NO": 15, "PK": 24, "PS": 29,
|
||||
"PL": 28, "PT": 25, "RO": 24, "SM": 27, "SA": 24, "RS": 22, "SK": 24, "SI": 19,
|
||||
"ES": 24, "SE": 24, "CH": 21, "TN": 24, "TR": 26, "AE": 23, "GB": 22, "VG": 24
|
||||
} as $lengths
|
||||
# Ignore spaces and tabs, and check input is ALPHAnumeric:
|
||||
| gsub("[ \t]";"")
|
||||
| test("^[A-Z0-9]+$")
|
||||
# Validate country code against expected length:
|
||||
and length == $lengths[.[0:2]]
|
||||
# Shift and convert:
|
||||
and ( (.[4:] + .[0:4]) | letters2digits | remainder) == 1 ;
|
||||
2
Task/IBAN/jq/iban-3.jq
Normal file
2
Task/IBAN/jq/iban-3.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
"GB82 WEST 1234 5698 7654 32" | is_valid_iban #=> true
|
||||
"GB82 TEST 1234 5698 7654 32" | is_valid_iban #=> false
|
||||
Loading…
Add table
Add a link
Reference in a new issue