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] FW: Splitting a number into even- and odd- numbered digits
by Terry Carroll other posts by this author
Apr 20 2006 10:14AM messages near this date
[Tutor] FW: Splitting a number into even- and odd- numbered digits | [Tutor] Config file parsing
On Thu, 20 Apr 2006, Carroll, Barry wrote:

>  > 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.
>   
>  I forgot to include two important requirements:
>  
>      1. the length of the input string is arbitrary,
>      2. the order of the digits must be maintained.
>  
>  I could not find a way to include these requirements in a single, simple
>  expression.  

I really liked John Fouhy's approach, or at least a variation of it:

> >> def odd_even(s):
...    '''
...    Returns a tuple of two strings taken from s.  The first string is
...    the odd-numbered characters (counting from the right), and the
...    second is the even-numbered characters.
...    '''
...    return (s[::-2][::-1], s[-2::-2][::-1])
...
> >> odd_even("1234567")
('1357', '246')
> >> odd_even("2345")
('35', '24')
> >> odd_even("1")
('1', '')
> >>

John's original solution returned values with the digits in reverse 
order; but that's easily changed by reslicing each string with [::-1].

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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved