[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Looping through the gmail dot trick

Gabriel Genellina

1/20/2008 8:15:00 PM

En Sun, 20 Jan 2008 14:38:06 -0200, Joshua Gilman <joshuagilman@gmail.com>
escribi�:

> My task is this: Loop through an email and create as many combinations of
> periods as possible. So all the combinations for blah would be:
>
> b.lah
> bl.ah
> bla.h
> b.l.ah
> b.la.h
> bl.a.h

I'd use a recursive generator (the divide-and-conquer approach):

def genalldots(txt):
if len(txt)<=1:
yield txt
else:
head, tail = txt[0], txt[1:]
for item in genalldots(tail):
yield head+item
yield head+'.'+item

print sorted(genalldots('blah'))

(I got your six spellings above, plus 'blah' and 'b.l.a.h')

> I'm still rather new to python so this is turning out to be rather
> tricky.
> My current code is as follows:
>
> for d in range(1, len(email)):
>> for i in range(1, len(email)):
>> y = i
>> temail = email
>> for x in range(d):
>> if email[y] == '.': break
>> temail = temail.replace(email[y], '.' + email[y])
>> if not y > len(email) - 2: y += 1
>> print temail

The replace function is dangerous, in case a letter appears more than
once, you are replacing all instances. Anyway, since you *know* you want
to replace the y-th item, just do:
temail = temail[:y] + '.' + temail[y:]

--
Gabriel Genellina