Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39277
| References | <CAJ2vgs5uzr1BHF-koYmb5s0tBSkzChTKSQ8k=pCkVC21LWXoAQ@mail.gmail.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2013-02-19 14:10 -0700 |
| Subject | Re: Verification of bank number using modulus 11 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2063.1361308285.2939.python-list@python.org> (permalink) |
On Tue, Feb 19, 2013 at 1:50 PM, Morten Engvoldsen <mortenengv@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)".
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Verification of bank number using modulus 11 Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-19 14:10 -0700
csiph-web