Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Verification of bank number using modulus 11

1,836 views
Skip to first unread message

Morten Engvoldsen

unread,
Feb 19, 2013, 3:50:18 PM2/19/13
to pytho...@python.org
Hi Team,
I am trying to verify the account number using the following algorithm:
 
"The valid account number is 11 numeric digit without seperator. Eg. 86011117947 is a valid account number. All banks apply a modulus-based method for the validation of the account structure. The 10-digit account number is multiplied from left to right by the following weights: 5, 4, 3, 2, 7, 6, 5, 4, 3, 2. The resulting numbers are added up and divided by 11. The remainder is subtracted from 11 and becomes the check digit. If the remainder is 0, the check digit will be 0. If digits 5 and 6 of the account number are zeros, the check digit is calculated on the 7, 8, 9 and 10th digit of the account number. Account numbers for which the remainder is 1 (check digit 10) cannot be used."
 
 I am trying to validate the Norway account number using the algorithm mentioned in the following document:
 
Here is my code:
 def calc_checkdigit(isbn):
        isbn = isbn.replace(".", "")
        check_digit = int(isbn[-1])
        isbn = isbn[:-1]
        if len(isbn) != 10:
               return False
        result = sum((10 - i) * (int(x) if x != 'X' else 10) for i, x in enumerate(isbn))
        return (result % 11) == check_digit
 
calc_checkdigit(""8601.11.17947"")
 
In my program : it is calculating 10 digit with weights 10-1, but according to above algorithm the weights should be : 5, 4, 3, 2, 7, 6, 5, 4, 3, 2. 
 
Could you please let me know how can i calculate the 10 digit with weights 5, 4, 3, 2, 7, 6, 5, 4, 3, 2. I am using python 2.7.
 
Thanks in advance team for help..
 
 

 

 

):

 

 

Dave Angel

unread,
Feb 19, 2013, 4:10:54 PM2/19/13
to pytho...@python.org
Without analyzing your code, the direct answer to your code would be to
make a table, something like:

weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]

and multiple by weights[i] rather than (10-i)




--
DaveA

Ian Kelly

unread,
Feb 19, 2013, 4:10:42 PM2/19/13
to Python
On Tue, Feb 19, 2013 at 1:50 PM, Morten Engvoldsen <morte...@gmail.com> wrote:
> Here is my code:
> def calc_checkdigit(isbn):
> isbn = isbn.replace(".", "")
> check_digit = int(isbn[-1])
> isbn = isbn[:-1]
> if len(isbn) != 10:
> return False
> result = sum((10 - i) * (int(x) if x != 'X' else 10) for i, x in
> enumerate(isbn))
> return (result % 11) == check_digit
>
> calc_checkdigit(""8601.11.17947"")
>
> In my program : it is calculating 10 digit with weights 10-1, but according
> to above algorithm the weights should be : 5, 4, 3, 2, 7, 6, 5, 4, 3, 2.
>
> Could you please let me know how can i calculate the 10 digit with weights
> 5, 4, 3, 2, 7, 6, 5, 4, 3, 2. I am using python 2.7.

Use zip with the weight sequence instead of enumerate:

weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]
result = sum(w * (int(x) if x != 'X' else 10) for w, x in
zip(weights, isbn))

Do the account numbers actually use 'X', or is that just left over
from the ISBN algorithm? If not, then you could replace "(int(x) if x
!= 'X' else 10)" with just "int(x)".

MRAB

unread,
Feb 19, 2013, 4:12:29 PM2/19/13
to pytho...@python.org
On 2013-02-19 20:50, Morten Engvoldsen wrote:
> Hi Team,
> I am trying to verify the account number using the following algorithm:
>
> "The valid account number is 11 numeric digit without seperator. Eg.
> 86011117947 is a valid account number. All banks apply a modulus-based
> method for the validation of the account structure. The 10-digit account
> number is multiplied from left to right by the following weights: 5, 4,
> 3, 2, 7, 6, 5, 4, 3, 2. The resulting numbers are added up and divided
> by 11. The remainder is subtracted from 11 and becomes the check digit.
> If the remainder is 0, the check digit will be 0. If digits 5 and 6 of
> the account number are zeros, the check digit is calculated on the 7, 8,
> 9 and 10th digit of the account number. Account numbers for which the
> remainder is 1 (check digit 10) cannot be used."
>
> I am trying to validate the Norway account number using the algorithm
> mentioned in the following document:
> http://www.cnb.cz/miranda2/export/sites/www.cnb.cz/cs/platebni_styk/iban/download/TR201.pdf
>
> Here is my code:
> def calc_checkdigit(isbn):
> isbn = isbn.replace(".", "")
> check_digit = int(isbn[-1])
> isbn = isbn[:-1]
> if len(isbn) != 10:
> return False
> result = sum((10 - i) * (int(x) if x != 'X' else 10) for i, x in enumerate(isbn))
> return (result % 11) == check_digit
>
> calc_checkdigit(""8601.11.17947"")
>
> In my program : it is calculating 10 digit with weights 10-1, but
> according to above algorithm the weights should be : 5, 4, 3, 2, 7, 6,
> 5, 4, 3, 2.
>
> Could you please let me know how can i calculate the 10 digit with
> weights 5, 4, 3, 2, 7, 6, 5, 4, 3, 2. I am using python 2.7.
>
> Thanks in advance team for help..
>
Put the weights into a list and use 'zip' instead of 'enumerate':

sum(w * (10 if d == 'X' else int(d)) for w, d in zip(weights, isbn))

Morten Engvoldsen

unread,
Feb 19, 2013, 5:59:34 PM2/19/13
to ian.g...@gmail.com, pyt...@mrabarnett.plus.com, pytho...@python.org
Hi Team,
Thanks for the code.
 I have altered the code with below code, it is able to validate the number now,
 
def calc_checkdigit(isbn):
isbn = isbn.replace(".", "")
check_digit = int(isbn[-1])
isbn = isbn[:-1]
if len(isbn) != 10:
  return False
weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]
result = sum(w * (int(x)) for w, x in zip(weights, isbn))
remainder = result % 11
if remainder == 0:
   check = 0
else:
   check = 11 - remainder
return check == check_digit
 
calc_checkdigit(""8601.11.17947"")
 
 
But can you tell me how could i implement below

"If digits 5 and 6 of the account number are zeros, the check digit is calculated on the 7, 8, 9 and 10th digit of the account number."

which means if account number is "8601.00.17947" then check digit is calculate as 

 result = (1*5) + (7*4)+ (9*3)+(4*2)

remainder = result % 11

check_digit = 11 - remainder

Can you tell me how can i implement this ?

 


 

 

Ian Kelly

unread,
Feb 19, 2013, 6:21:35 PM2/19/13
to Python
On Tue, Feb 19, 2013 at 3:59 PM, Morten Engvoldsen <morte...@gmail.com> wrote:
> But can you tell me how could i implement below
>
> "If digits 5 and 6 of the account number are zeros, the check digit is
> calculated on the 7, 8, 9 and 10th digit of the account number."
>
> which means if account number is "8601.00.17947" then check digit is
> calculate as
>
> result = (1*5) + (7*4)+ (9*3)+(4*2)
>
> remainder = result % 11
>
> check_digit = 11 - remainder
>
> Can you tell me how can i implement this ?

After this code:

isbn = isbn[:-1]
if len(isbn) != 10:
return False

Add:

if isbn[4:6] == "00":
isbn = isbn[6:]

And that should do it. Thanks to the symmetry of the weights
sequence, you don't even need to change that part at all. The zip
function will automatically truncate to the length of the shorter
input sequence.

Morten Engvoldsen

unread,
Feb 20, 2013, 4:24:31 AM2/20/13
to ian.g...@gmail.com, pytho...@python.org
Hi,
Thanks for below code:

After this code:

isbn = isbn[:-1]
if len(isbn) != 10:
  return False

Add:

if isbn[4:6] == "00":
    isbn = isbn[6:]

It is working exactly how it should :)
I didn't even change that part .  The zip function automatically truncates to the length of the shorter input sequence.

Thanks a lot again :)

Have a nice day..


On Tue, Feb 19, 2013 at 11:59 PM, Morten Engvoldsen <morte...@gmail.com> wrote:
Hi Team,
Thanks for the code.
 I have altered the code with below code, it is able to validate the number now,
def calc_checkdigit(isbn):
isbn = isbn.replace(".", "")
check_digit = int(isbn[-1])
isbn = isbn[:-1]
if len(isbn) != 10:
  return False
weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]
result = sum(w * (int(x)) for w, x in zip(weights, isbn))
remainder = result % 11
if remainder == 0:
   check = 0
else:
   check = 11 - remainder
return check == check_digit
 
calc_checkdigit(""8601.11.17947"")
 
 
0 new messages