Number validation issue

Morten Engvoldsen mortenengv at gmail.com
Sat Feb 23 04:11:04 EST 2013


>
> Hi,
>
 Yes true, i need to generate weight at run time rather than hard coding it,
following are some solution:

Since the max length of thenumber should be 25 and minimum should be 2 so
code will be in this way:

def is_valid_isbn13(isbn13):
    if not len(checknum) >= 2 and len(checknum) <=25:
            return False
    weigth = '1' + ''.join(map(str,range(2,8))*4)
    ex_weigth = list(reversed(list(weigth)[: int(len(isbn13))]))
    return (sum(int(w) * int(x) for w, x in zip(list(ex_weigth),
list(isbn13))) % 11 == 0)

is_valid_isbn13("123456785")

This code works fine for any length of digit between 2 to 25, this is what
exactly i need, is there any other better approch you have to generate the
weight at run time.









>
> ---------- Forwarded message ----------
> From: Dave Angel <davea at davea.name>
> To: python-list at python.org
> Cc:
> Date: Fri, 22 Feb 2013 13:45:30 -0500
> Subject: Re: Number validation issue
> On 02/22/2013 01:27 PM, Morten Engvoldsen wrote:
>
>> Hi,
>> Just to make it more clear: I am looking for how to generate the weight
>> in :
>> 1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7.. format for any
>> length of number instead of
>>
>> weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1]
>>
>> only for fixed digit.
>>
>> My below code can check only for 9 digit, so if we provide a number of
>> more
>> than 9 digit, it is not able to check that number. Hope, this makes clear
>> what i am looking for...
>>
>> You keep top-posting, so we keep losing the context.
>
> zip() stops at the shorter of the two iterables. And itertools.cycle()
> will repeat a list infinitely. The two of them should be able to solve your
> problem.
>
>
> --
> DaveA
>
>
>
> ---------- Forwarded message ----------
> From: Ian Kelly <ian.g.kelly at gmail.com>
> To: Python <python-list at python.org>
> Cc:
> Date: Fri, 22 Feb 2013 11:50:13 -0700
> Subject: Re: Number validation issue
> On Fri, Feb 22, 2013 at 11:27 AM, Morten Engvoldsen
> <mortenengv at gmail.com> wrote:
> > Hi,
> > Just to make it more clear: I am looking for how to generate the weight
> in :
> > 1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7.. format for any
> > length of number instead of
> >
> > weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1]
> >
> > only for fixed digit.
> >
> > My below code can check only for 9 digit, so if we provide a number of
> more
> > than 9 digit, it is not able to check that number. Hope, this makes clear
> > what i am looking for...
>
> It sounds like you probably should generate the list of weights at
> run-time rather than hard-coding it as a literal. Have you tried
> this?
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130223/fd41b509/attachment.html>


More information about the Python-list mailing list