silly idea - interesting problem

scott smarsh at hotmail.com
Sun Oct 21 07:02:35 CEST 2001


Magnus Lie Hetland wrote:
> 
> "scott" <smarsh at hotmail.com> wrote in message
> news:3BD248BA.2163BBDB at hotmail.com...
> >
> > Stefan Antoni wrote:
> > >
> > > i got a silly idea about a small script which takes to strings (lets say
> > > "test" and "dust") and mixes them to "tdeusstt".
> > > i tried to code it because i thought it would be easy, but now i cannot
> > > find out _how_ to do it ;)
> > >
> > > i tried to "list()" the string and mix the items. but ...
> > >
> > > Anybody has a hint or even a solution?
> > >
> >
> > Here's a back to basics solution:
> >
> > firstWord = 'test'
> > secondWord = 'dust'
> > i = 0
> > while i < len(firstWord):
> >     print firstWord[i], secondWord[i],
> >     i = i+1
> 
> This will give spaces between every word...

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 

-- 
Colorless green ideas sleep furiously. 
     Chomsky



More information about the Python-list mailing list