First script, please comment and advise

Pedro Graca hexkid at dodgeit.com
Thu Mar 9 09:17:02 EST 2006


I'm sure this isn't very pythonic; comments and advice appreciated


    def curious(text):
        """ Return the words in input text scrambled except for the first and last letter. """
        new_text = ""
        word = ""
        for ch in text:
            if ch in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
                word = word + ch
            else:
                new_text = new_text + scramble(word)
                word = ""
                new_text = new_text + ch
        return new_text
    
    def scramble(word):
        """ scramble word """
        from random import randint
    
        if len(word) < 4:
            return word
        new_word = word[0]
    
        ### transform "curious" into ['u', 'r', 'i', 'o', 'u']
        letters = []
        for ch in word:
            letters.append(ch)
        del letters[0:1]
        del letters[-1]
    
        ### why doesn't range(len(letters) - 1, 0, -1) work?
        for i in range(len(letters) - 1, -1, -1):
            j = randint(0, i)
            new_word = new_word + letters[j]
            del letters[j]
        return new_word + word[-1]
    
    print curious(curious.__doc__)


-- 
If you're posting through Google read <http://cfaj.freeshell.org/google>



More information about the Python-list mailing list