IBAN
October 25, 2019
The International Bank Account Number (IBAN) is an internationally agreed standard of identifying bank accounts with a hash number to reduce errors. The first two characters of an IBAN are the two-letter country code, the next two characters are a two-digit hash, and the remaining characters identify the bank routing number and depositor account number. For instance, here is a fictition British IBAN: GB82 WEST 1234 5698 7654 32. The full IBAN standard specifies the range of valid bank account numbers and depositor account numbers for each country; we are interested only in the hash code.
In the code shown above, the hash code consists of the two digits 82, which are validated as follows:
1) Move the first four characters from the beginning of the string to the end: WEST 1234 5698 7654 32GB 82.
2) Replace letters with two-digit numbers according to the scheme A = 10, …, Z = 35:
3214 2829 1234 5698 7654 3216 1182.
3) Treating the string as a large integer, divide by 97 and calculate the remainder.
4) If the remainder is not 1, the IBAN is not valid.
To generate a hash code, perform the same procedure as above with the two hash digits initially set to 00, then subtract the hash code from 98 and insert it in the IBAN.
Your task is to write functions to validate existing IBAN numbers and generate hash codes for new IBAN numbers. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Python 3.8 solution:
Used as follows:
Here is a simple solution in R7R6 Scheme and a few helpers from
popular SRFIs.
It emphasizes clarity over efficiency and makes rather liberal use of
potentially expensive string operations and conversions. On the other
hand, given the fixed size and format of IBAN, it is a Theta(1)
solution.
It also allows human-friendly spaces (and potentially other
punctuation) in the IBANs.
Output:
I’m not going to try to compete with Alex’s nicely engineered solution, so here’s a Python two-liner (it would go into one, but it feels like cheating to go over 80 chars in a line):
And in fact, we can use the Python string -> int conversion with base 36 and get an under 80 chars one-liner (I got this from the Rosetta Code IBAN page):
A Haskell version.
When I tried to run your solution in Gambit Scheme, I got the following:
(define-syntax let-values
(syntax-rules ()
((_ () f1 f2 …) (let () f1 f2 …))
((_ ((fmls1 expr1) (fmls2 expr2) …) f1 f2 …)
(let-values-help fmls1 () () expr1 ((fmls2 expr2) …) (f1 f2 …)))))
*** ERROR IN (console)@56.17 — Ill-formed expression
3> (define-syntax let-values-help
(syntax-rules ()
((_ (x1 . fmls) (x …) (t …) e m b)
(let-values-help fmls (x … x1) (t … tmp) e m b))
((_ () (x …) (t …) e m b)
(call-with-values
(lambda () e)
(lambda (t …)
(let-values m (let ((x t) …) . b)))))
((_ xr (x …) (t …) e m b)
(call-with-values
(lambda () e)
(lambda (t … . tmpr)
(let-values m (let ((x t) … (xr tmpr)) . b)))))))
*** ERROR IN (console)@61.17 — Ill-formed expression
Here’s a solution in C.
Example usage:
calc_hash
is not a fitting name for the function above. I think that reflects my original intent, although the function calculates a remainder, not a hash.