Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'algorithm': 0.03; 'python': 0.09; 'calculating': 0.09; 'subject:number': 0.09; 'subject:using': 0.09; 'def': 0.10; '(result': 0.16; 'isbn': 0.16; 'wrote:': 0.17; 'feb': 0.19; 'not,': 0.21; 'header:In-Reply-To:1': 0.25; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'could': 0.32; '"")': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'false': 0.35; 'sequence': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'here': 0.65; 'account': 0.67; '2.7.': 0.84; '2013': 0.84; 'subject:Verification': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=U2ReWiXhyW5hjs5fFmBGNMNFmNNeZVrWufc4hDkgaVE=; b=R4SG0zJkg9ItVDATvIZyM3L51IgT8RyU9JrYBvXNr2WPHKjqa2GM3PtbD2bTWfUFrX +qA4YZyYzCJknZLdZYjDlhq45e7n6IcWqOHGpYQM4JOqWZh4kw+KPUt7GqArhHMzKzzS /wfhSDIVUEPPuxdUFkP4cIFWpbn014VYzU7oMBYYanZZ5VkfCaDKArJ19KE40qQoDaKX YsHBrqYn3h2SYtyh+6hQA5qJmSDYoNak8u2uAYsH83pBwfyn7TMHgQHw3aumYEVx4Di1 pMQH/fUdFXMGOLlhlziXoEvoh6a6Qk6/QwhjCueFsKIBWhSJ2Xd03lPAAyegFaza9tap yzog== X-Received: by 10.68.135.68 with SMTP id pq4mr43989551pbb.12.1361308282988; Tue, 19 Feb 2013 13:11:22 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Tue, 19 Feb 2013 14:10:42 -0700 Subject: Re: Verification of bank number using modulus 11 To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361308285 news.xs4all.nl 6867 [2001:888:2000:d::a6]:35616 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39277 On Tue, Feb 19, 2013 at 1:50 PM, Morten Engvoldsen 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)".