Help me understand this script

Ryan J Nauman RJNauman at uss.com
Wed Sep 5 07:45:06 EDT 2007


Can someone help me understand this script please?
I understand everything except for the anagram function.  Could you break 
it down into more than 1 line of code for me or explain it? (I understand 
WHAT it does but HOW?) Thanks.

Script >>

###
# SCRABBLE.PY
#
# purpose: 
#       find usable "bingos" based on your rack
#
# usage:
#       python scrabble.py eilsnsy
#
# output:
# Straight anagrams: 
#  linseys
#  lysines
# Possible other words: 
# + B
#   sensibly
#+ K
#   skylines
#+ V
#   sylvines
###
# Scrabble is a registered trademark of J. W. Spear & Son PLC and 
# Hasbro Inc. Any and all uses of the word "Scrabble" in this code
# refers to this trademark.
#
# This code is not affiliated with any company.
###

import sys

WORDS = [ i.rstrip ().lower() for i in file ('c:\python25\TWL06.txt') ]
# you can download the current TWL and/or SOWPODS dictionaries from 
# http://67.19.18.90/twl.zip and http://67.19.18.90/sowpods.zip . 
# Update the file name above as appropriate if you want to use a "proper"
# Scrabble dictionary.



def alphabetise(word):
    x = [i for i in word]
    x.sort()
 
    return "".join(x)

def anagram(word):
    wordLength = len(word)
    sortedWord = alphabetise(word.lower())
    return [i for i in WORDS if len(i) == wordLength and alphabetise(i) == 
sortedWord]

for word in sys.argv[1:]:
    print "Straight anagrams: "
    for i in anagram(word):
        print "  " + i
    print "Possible other words: "
    for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
        a = anagram(i + word)
        if a:
            print "+", i
            for h in a:
                print "  ", h

<< End script
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070905/c6e9af39/attachment.html>


More information about the Python-list mailing list