[Tutor] More Pythonesque or a more efficient method
Kent Johnson
kent37 at tds.net
Sun Oct 5 20:54:04 CEST 2008
On Sun, Oct 5, 2008 at 1:52 PM, Robert Berman <bermanrl at embarqmail.com> wrote:
> The database item consists of the key; the actual word, and the value, the
> size as a string. For example, the word 'myth' is represented as key=
> 'myth', value = '4'. I think the slow part of the algorithm is the script
> retrieves a list of all database words of length (myth)=4. That's a lot of
> words. Each word is sorted and then compared to the alpha sorted word. When
> there is a match, that word becomes a part of the anagram list. Even a very
> basic look at the word 'myth' shows there are no anagrams. So, to return an
> empty list, we have scanned all words in the database of size 4.
This is pretty inefficient, yes. You actually scan every word in the
database because you have to find the ones of length 4. You are really
using the database as just a list of words. A better data structure
would be to index by word length with the value being a list of words
of that length. Even better would be to index by the sorted letters in
the word, with the value being a list of all words with that sorting.
Kent
More information about the Tutor
mailing list