Re: [Tutor] Splitting a number into even- and odd- numbered digits
Greetings: Unfortunately, my problem description was incomplete. 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 decided to make an explicit test for string length, which simplified matters. I came up with this: >>> >>> def odd_even(x): ... if len(x) % 2 == 1: ... return x[::2], x[1::2] ... else: ... return x[1::2], x[::2] >>> odd_even('987654321') ('97531', '8642') >>> odd_even('98765432') ('8642', '9753') >>> >>> which is an improvement, I think, on my original. Thanks to those who provided suggestions. This is the most useful list I've ever found. Regards, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Splitting a number into even- and odd- numbered digits
Greetings: First of all, thanks to those who contributed suggestions. Unfortunately, my description was incomplete. > 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. 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 decided to make an explicit test for string length, which simplified matters. I came up with this: >>> >>> def odd_even(x): ... if len(x) % 2 == 1: ... return x[::2], x[1::2] ... else: ... return x[1::2], x[::2] >>> odd_even('987654321') ('97531', '8642') >>> odd_even('98765432') ('8642', '9753') >>> >>> which is an improvement, I think, on my original. > >>> > >>> 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' > >>> Thanks again. This is the most useful list I've ever found. Regards, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
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 > [EMAIL PROTECTED] > 541-302-1107 > > We who cut mere stones must always be envisioning cathedrals. > -Quarry worker's creed > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Lloyd Kvam Venix Corp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Splitting a number into even- and odd- numbered digits
On 20/04/06, Carroll, Barry <[EMAIL PROTECTED]> wrote: > 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 > ... > >>> String slicing will actually produce strings :-) eg: >>> s = '987654321' >>> s1, s2 = s[::-2], s[-2::-2] >>> s1 '13579' >>> s2 '2468' -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor