permuting letters and fairy tales

Peter Otten __peter__ at web.de
Sun Nov 14 03:49:31 EST 2004


Bengt Richter wrote:

> It really deserves to be more than an acquaintance.  It's surprising that
> itertools doesn't show up in posted solutions more (Alex's notwithstanding
> ;-)

I can help fix that :-)

While I think that a good old for-loop is often more readable than the
"cool" itertools solution, itertools has just grown a new function that
fits into your messwith() generator quite nicely:

def messwith(s):
    for isalpha, chars in itertools.groupby(s, type(s).isalpha):
        chars = list(chars)
        if isalpha and len(chars) > 3:
            mid = chars[1:-1]
            random.shuffle(mid)
            chars[1:-1] = mid
        yield "".join(chars)

Peter





More information about the Python-list mailing list