Ryan J Nauman wrote: >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 > > I guess the difficult part remains in the last line. (Untested code bellow) [code]
def anagram(word): resultValue = [] wordLength = len(word) sortedWord = alphabetise(word.lower()) #not much to change until here for WORD in WORDS: if wordLength == len(WORD) and alphabetise(WORD) == sortedWord: resultValue.append(WORD) return resultValue [/code] Cheers. Gerardo -- http://mail.python.org/mailman/listinfo/python-list