IBAN: Difference between revisions

356 bytes removed ,  4 years ago
→‎{{header|Go}}: Fix Go program to be of acceptable style. The previous code was inacceptably structured and didn't even produce the given output.
(→‎{{header|C++}}: format the code in a more common way)
(→‎{{header|Go}}: Fix Go program to be of acceptable style. The previous code was inacceptably structured and didn't even produce the given output.)
Line 1,808:
=={{header|Go}}==
 
<lang Go>package main
package main
 
import (
"fmtregexp"
"strings"
"strconvtesting"
"math/big"
)
 
var lCodelengthByCountryCode = map[string]int {
"AL": 28, "AD": 24, "AT": 20, "AZ": 28, "BE": 16, "BH": 22, "BA": 20, "BR": 29,
"BGBA": 2220, "CRBR": 2129, "HRBG": 2122, "CYCR": 2821, "CZHR": 2421, "DK": 18, "DOCY": 28, "EE": 20,
"FOCZ": 1824, "FIDK": 18, "FRDO": 2728, "GEEE": 2220, "DEFO": 2218, "GI": 23, "GR": 27, "GLFI": 18,
"GTFR": 2827, "HUGE": 2822, "IS": 26, "IEDE": 22, "ILGI": 23, "ITGR": 27, "KZ": 20, "KWGL": 3018,
"LVGT": 2128, "LBHU": 28, "LI": 21, "LT": 20, "LUIS": 2026, "MKIE": 1922, "MTIL": 3123, "MRIT": 27,
"MUKZ": 3020, "MCKW": 27, "MD": 24, "ME": 2230, "NLLV": 1821, "NOLB": 1528, "PKLI": 2421, "PSLT": 2920,
"PLLU": 2820, "PTMK": 2519, "ROMT": 2431, "SMMR": 27, "SAMU": 2430, "RSMC": 22, "SK": 24, "SI": 1927,
"ESMD": 24, "SEME": 2422, "CHNL": 2118, "TNNO": 2415, "TRPK": 2624, "AEPS": 23, "GB": 22, "VG": 2429,
"OPL": 2428, "PPT": 25, "QRO": 2624, "RSM": 27, "SSA": 28, "T": 2924, "URS": 3022,
"HSK": 1724, "ISI": 1819, "JES": 1924, "KSE": 2024, "LCH": 21, "M": 22, "NTN": 2324,
"VTR": 3126, "WAE": 3223, "XGB": 3322, "YVG": 34, "Z": 3524,
}
 
varfunc sCodeisValidIBAN(iban = map[string]int) bool {
if len(iban) < 4 {
"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
return false
"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15, "G": 16,
}
"H": 17, "I": 18, "J": 19, "K": 20, "L": 21, "M": 22, "N": 23,
iban = regexp.MustCompile(`\s+`).ReplaceAllString(iban, "")
"O": 24, "P": 25, "Q": 26, "R": 27, "S": 28, "T": 29, "U": 30,
riban = strings.SplitToUpper(iban, " ")
"V": 31, "W": 32, "X": 33, "Y": 34, "Z": 35,
 
ibanLen := lengthByCountryCode[iban[0:2]]
func main() {
if ibanLen == 0 {
return false
var iban string
var r, s, t, st []string
u := new(big.Int)
v := new(big.Int)
w := new(big.Int)
iban = "GB82 TEST 1234 5698 7654 32"
r = strings.Split(iban, " ")
s = strings.Split(r[0], "")
t = strings.Split(r[1], "")
st = []string{ strconv.Itoa(sCode[t[0]]),
strconv.Itoa(sCode[t[1]]),
strconv.Itoa(sCode[t[2]]),
strconv.Itoa(sCode[t[3]]),
strings.Join(r[2:6], ""),
strconv.Itoa(sCode[s[0]]),
strconv.Itoa(sCode[s[1]]),
strings.Join(s[2:4], ""),
}
iban = iban[4:] + iban[:4]
 
result := 0
u.SetString(strings.Join(st, ""), 10)
for _, ch := range iban {
v.SetInt64(97)
var n int
w.Mod(u, v)
if '0' <= ch && ch <= '9' {
n = int(ch) - '0'
if w.Uint64() == 1 && lCode[strings.Join(s[0:2], "")] == len(strings.Join(r, "")) {
} else if 'A' <= ch && ch <= 'Z' {
fmt.Printf("IBAN %s looks good!\n", iban)
n = 10 + int(ch) - 'A'
} else {
fmt.Printf("IBAN %s looks wrong!\n", iban)
return false
}
 
if n < 10 {
result = (10*result + n) % 97
} else {
result = (100*result + n) % 97
}
}
return result == 1
}
</lang>
 
func TestIsValidIBAN(t *testing.T) {
<pre>
tests := []struct {
IBAN GB82 WEST 1234 5698 7654 32 looks good!
var iban string
IBAN GB82 TEST 1234 5698 7654 32 looks wrong!
valid bool
IBAN CH93 0076 2011 6238 5295 7 looks good!
}{
</pre>
IBAN {"GB82 WEST 1234 5698 7654 32", looks good!true},
iban = {"GB82 TEST 1234 5698 7654 32", false},
}
for _, test := range tests {
if isValidIBAN(test.iban) == test.valid {
return
}
t.Errorf("Expected %q to be %v", test.iban, test.valid)
}
}
</lang>
 
=={{header|Groovy}}==