Re: [Tutor] Tutor Digest, Vol 26, Issue 71
by Barry Carroll other posts by this author
Apr 20 2006 11:25AM messages near this date
1
|
[Tutor] Apology (Was: Re: Splitting a number into even- and odd- numbered digits)
Terry:
> -----Original Message-----
> Date: Thu, 20 Apr 2006 10:14:23 -0700 (PDT)
> From: Terry Carroll <carroll@[...].com>
> Subject: Re: [Tutor] FW: Splitting a number into even- and odd-
> numbered digits
> To: tutor@[...].org
> Message-ID:
> <Pine.LNX.4.44.0604201000430.19166-100000@[...].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@[...].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
|