How to resolve the algorithm Validate International Securities Identification Number step by step in the Tcl 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 Tcl 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 Tcl programming language
Source code in the tcl programming language
package require Tcl 8.6 ;# mostly needed for [assert]. Substitute a simpler one or a NOP if required.
proc assert {expr} { ;# for "static" assertions that throw nice errors
if {![uplevel 1 [list expr $expr]]} {
set msg "{$expr}"
catch {append msg " {[uplevel 1 [list subst -noc $expr]]}"}
tailcall throw {ASSERT ERROR} $msg
}
}
namespace eval isin {
proc _init {} { ;# sets up the map used on every call
variable map
set alphabet abcdefghijklmnopqrstuvwxyz
set n 9
lmap c [split $alphabet ""] {
lappend map $c [incr n]
}
}
_init
proc normalize {isin} {
variable map
string map $map [string tolower [string trim $isin]]
}
# copied from "Luhn test of credit card numbers"
# included here for ease of testing, and because it is short
proc luhn digitString {
if {[regexp {[^0-9]} $digitString]} {error "not a number"}
set sum 0
set flip 1
foreach ch [lreverse [split $digitString {}]] {
incr sum [lindex {
{0 1 2 3 4 5 6 7 8 9}
{0 2 4 6 8 1 3 5 7 9}
} [expr {[incr flip] & 1}] $ch]
}
return [expr {($sum % 10) == 0}]
}
proc validate {isin} {
if {![regexp {^[A-Z]{2}[A-Z0-9]{9}[0-9]$} $isin]} {return false}
luhn [normalize $isin]
}
}
package require tcltest
tcltest::test isin-1 "Test isin validation" -body {
foreach {isin ok} {
US0378331005 yes
US0373831005 no
U50378331005 no
US03378331005 no
AU0000XVGZA3 yes
AU0000VXGZA3 yes
FR0000988040 yes
} {
if {$ok} {
assert {[isin::validate $isin]}
} else {
assert {![isin::validate $isin]}
}
}
return ok
} -result ok
You may also check:How to resolve the algorithm Five weekends step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Julia programming language
You may also check:How to resolve the algorithm Arrays step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the C programming language