Instant Programming with Python

6 AM February 11, 2003

My darling wife was telling me how much fun she had yesterday at work trying to make words out of the letters of "serendipitous". The group she was running managed 100. I agreed that sounded like fun and immediately set to writing an instant program to find more than her group.

Step 1. Google up a word list and download it:


[alang@homer words]$ wc -l ShortList.txt

  36720 ShortList.txt

[alang@homer words]$

Step 2. Write program:


# Make words



from xreadlines import xreadlines



def contains(needle, haystack):

    t = []

    t.extend(haystack)

    for c in needle:

        if c in t:

            t.remove(c)

        else:

            return 0

    return 1





f = file('ShortList.txt')



for word in xreadlines(f):

    word = word.strip()

    if len(word) > 4 and \

    contains(word, "serendipitous"):

        print word



Not very pretty, but quick to write.

Step 3. Run it:


[alang@homer words]$ time python words.py | wc -l

    521



real    0m0.549s

user    0m0.545s

sys     0m0.004s

[alang@homer words]$

Step 4. Gloat:

I got more than you! 521! And I took less than one second to do it!

If only I would use Python for good rather than evil.

By alang | # | Comments (0)
(Posted to Software Development)
© 2003-2006 Alan Green