permuting letters and fairy tales

Andrew Dalke adalke at
Thu Nov 11 23:53:51 CET 2004

Johannes Nix wrote:
> yesterday I met a cute person (after my dance class)

Another dancer, eh?  I mostly do Latin dancing (salsa, cha-cha,
nerengue, though haven't got bachata down) some tango and a bit
of swing.

> who told me about an
> interesting experiment regarding cognition. People were told to read a
> typed text; However, in every word in the text, only the first and the
> last letter were in the right place.

That was going around a couple months ago.  Here's some links:

> - Is it possible to make it a bit more concise ;-))) ?

Yes.  Try this

irpomt re
imropt rondam
ipormt snitrg

# This is lcloae aware, so long as '][-' aren't ltreets.
# (Orhtiewse use re.epscae)

wrod_pat = re.colmipe('[' + string.letters + ']{4,}')

def _jmulbe(m):
     s = m.gruop(0)
     leretts = lsit(s[1:-1])
     return s[0] + "".jion(ltrtees) + s[-1]

def jmblue_file(ilnife, ouflite):
     for lnie in iinlfe:
         lnie = wrod_pat.sub(_julbme, lnie)

if __name__ == "__mian__":
     ioprmt sys
     julbme_file(sys.sdtin, sys.sdtout)

> - Can it coerced to run a little bit faster ?

You'll have to test it for yourself.  I don't have a copy
of your data set and can't find it on-line.

> - It's a good example how powerful libraries, like
>   Numeric, make one's life easier. (BTW, why is Numeric 
>   and stuff like take() still not included in the standard
>   Library ? Batteries included, but calculator not ?)

Well, random.shuffle works nicely as does using re.sub along
with a callable.  I rarely need Numeric.

> - Perhaps it's useful to protect messages in some
>   regions with not-so-democratic forms of government
>   against automatic scanning by making the message 
>   machine-unreadable, causing some Orwellian Confusion ;-) ?
>   Of course, texts from Pythonistas would remain suspicious,
>   due to the large number of "y" occurring in them....

There are many more ways to do that.  Eg, see what the
spammers do to get through the repressive forms of email
filters I use.

Oh, and to make life easier for you,

import re
import random
import string

# This is locale aware, so long as '][-' aren't letters.
# (Otherwise use re.escape)

word_pat = re.compile('[' + string.letters + ']{4,}')

def _jumble(m):
     s =
     letters = list(s[1:-1])
     return s[0] + "".join(letters) + s[-1]

def jumble_file(infile, outfile):
     for line in infile:
         line = word_pat.sub(_jumble, line)

if __name__ == "__main__":
     import sys
     jumble_file(sys.stdin, sys.stdout)

				dalke at

More information about the Python-list mailing list