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@python.org http://mail.python.org/mailman/listinfo/tutor