ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Splitting a number into even- and odd- numbered digits
by Python other posts by this author
Apr 20 2006 5:05AM messages near this date
[Tutor] Splitting a number into even- and odd- numbered digits | Re: [Tutor] Splitting a number into even- and odd- numbered digits
On Wed, 2006-04-19 at 17:17 -0700, Carroll, Barry wrote:
>  Greetings:
>  
>  I am writing a function that accepts a string of decimal digits,
>  calculates a checksum and returns it as a single character string.  
>  The first step in the calculation is to split the input into two
>  strings: the even- and odd- numbered digits, respectively.  The least
>  significant digit is defined as odd.  
>  
This sounds like credit card checksum processing.  This is my code for
that:

def isbad(cardnumber):
    factors = ([2,1] * 8)[-len(cardnumber):]        # alternating factors of 2 and 1 ending 
with 1
    chkprods = [int(d)*f for (d,f) in zip(cardnumber,factors)]
    return sum(chkprods) % 10

This deviates quite a bit from your description and the description of
the algorithm that I was working from.  It was only after I had coded up
separate even/odd character lists and looked at what was going on that I
realized there was a much simpler way to describe the rules.

Hopefully, I am not off in left field here.

>  The following code fragment does the job but seems sort of brutish and inelegant to me:
>  
>  >>>>>>>
>  >>> s = '987654321'
>  >>> odd = ''
>  >>> for c in s[::-2]:
>  ...     odd = c + odd
>  ...     
>  >>> s = s[:-1]
>  >>> even = ''
>  >>> for c in s[::-2]:
>  ...     even = c + even
>  ...     
>  >>> odd
>  '97531'
>  >>> even
>  '8642'
>  >>>>>>>
>  
>  Is there a better (i.e. more Pythonic) way to do this?  
>  
>  Thanks in advance for all your help.
>  
>  Regards,
>   
>  Barry
>  barry.carroll@[...].com
>  541-302-1107
>  ________________________
>  We who cut mere stones must always be envisioning cathedrals.
>  -Quarry worker's creed
>  
>  
>  _______________________________________________
>  Tutor maillist  -  Tutor@[...].org
>  http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Barry Carroll
Python
Barry Carroll
Barry Carroll
John Fouhy

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved