silly idea - interesting problem

scott smarsh at hotmail.com
Sun Oct 21 12:47:50 EDT 2001


Steve Holden wrote:
> 
> "scott" <smarsh at hotmail.com> wrote ...
> >
> [ ... handles nit-picks ...]
> >
> > Since we are nit-picking, the spaces are actually between each letter
> > ;-)
> > The point I was trying to make was that spraying a new Python user with
> > marginally readable code might be good for your ego but not much help
> > for learning the basics. I admit that if my code actually met the
> > requirements my argument would have been a bit more persuasive <my first
> > *wink* god help me now> :-)
> >
> > Here is the (as specified) no space version:
> >
> > firstWord = 'test'
> > secondWord = 'dust'
> > endString = ''
> > i = 0
> > while i < len(firstWord):
> >     endString = endString + firstWord[i] + secondWord[i]
> >     i = i+1
> > print endString
> >
> 
> Surely much simpler and, dare I suggest, rather more pythonic also, to use a
> "for" loop? As in:
> 
> ...
> for i in range(len(firstWord)):
>     endString += firstWord[i] + secondWord[i]
> ...
> 

Oh oh, Steve is here. I will now defer to his wisdom. Out with the
'while', in with the 'for'. I'll still skip the += for clarity.

firstWord = 'test'
secondWord = 'dust'
endString = ''
for i in range(len(firstWord)):
    endString = endString + firstWord[i] + secondWord[i]
print endString 

That's as far as I will go. Before you know it you'll have me using
schmancy features like list comprehensions and lambdas. I'll go back to
my cave now... 

P.S.
Here is an obfuscated version too for programmers with carpal tunnel:

f,s,e = 'test','dust',''
for i in range(len(f)):e+=f[i]+s[i]
print e

C u ltr.

-- 
Colorless green ideas sleep furiously. 
     Chomsky



More information about the Python-list mailing list