[Tutor] Tutor Digest, Vol 26, Issue 71

Carroll, Barry Barry.Carroll at psc.com
Thu Apr 20 20:24:35 CEST 2006


Terry:

> -----Original Message-----
> Date: Thu, 20 Apr 2006 10:14:23 -0700 (PDT)
> From: Terry Carroll <carroll at tjc.com>
> Subject: Re: [Tutor] FW: Splitting a number into even- and odd-
> 	numbered digits
> To: tutor at python.org
> Message-ID:
> 	<Pine.LNX.4.44.0604201000430.19166-100000 at violet.rahul.net>
> Content-Type: TEXT/PLAIN; charset=US-ASCII
> 
<<snip>>
> >
> > 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].

By golly you're right!  That is a very slick solution.  It didn't occur
to me to re-reverse the strings like that.  Thanks to both you and John.


Regards,
 
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed




More information about the Tutor mailing list