Fsrit Propttoye: SCLABMRE

L. Sommerer lsommerer at sewardweb.com
Fri Sep 19 19:36:49 EDT 2003


On Wed, 17 Sep 2003 09:18:26 -0400, Frnak "Sealmbcrd Eggs" Lomax 
<lomax at pumpichank.com> wrote:

>
> I hvae jsut fhiseind rniedag a Shdlaost atrlcie from Mnoday, Sbepemetr
> 15th, aobut an ingtriniug sdtuy from Cgbdirmae Uinevtrsiy, dciinesrbg
> how wodrs with thier itienrors selmrbacd can siltl be rdbaleae.
> Bcaeuse of the tioeznme that I lvie in, an initettrment dfceet in my
> clbae medom, and the lneiignrg atfer-ecfefts of my lsat "mohsroum
> salad exntarvzagaa", I am aaylws two days bhneid on the Web.
>
> [snip]

I was really glad to read this post.  I worked on the same little bit of 
code one evening this week, but I didn't feel very good about the solution. 
 I haven't been coding in python long, and it was quite instructive to see 
how someone else approached the same problem.

Lloyd Sommerer

(Here's what I did in case anyone's interested.)
-----------------------------------------------------
from random import shuffle

def scramble_word(MyWord):
    #
    # If the word includes punctuation, save the punctuation for later use,
    # and remove it from the end of the word.
    #
    if MyWord[-1] in [".", ";", ":", "," ,"!", "?"]:
        Punctuation = MyWord[-1]
        MyWord = MyWord[0:-1]
    else:
        Punctuation = ""
    #
    # 1, 2 and 3 letter words don't need to be scrambled, so skip them.
    #
    if len(MyWord) < 4:
        return MyWord + Punctuation
    else:
        #
        # Carve out the middle and make a list out of it so we can shuffle 
it
        # (can't shuffle a string). Then use "reduce" to return the list as 
a
        # string.
        #
        Middle = MyWord[1:-1]
        MiddleList = list(Middle)
        shuffle(MiddleList)
        NewMiddle = reduce(concat_letters, MiddleList)
        return MyWord[0] + NewMiddle + MyWord[-1] + Punctuation

def scramble(SomeText):
     ScrambledList = [scramble_word(x) for x in SomeText.split()]
     return reduce(concat_words, ScrambledList)
    def concat_letters(x,y):
    return x+y

def concat_words(x,y):
    return x+" "+y

if __name__ == '__main__':
    print 'Usage: scramble("Insert some text here to scramble")'





More information about the Python-list mailing list