sorting question

Andrew Henshaw andrew_dot_henshaw_at_earthling_dot_net
Thu Feb 8 21:57:31 EST 2001


Depending upon your constraints, another way to approach this problem may be
better.  If you can get your word (or Word) dictionary, then you can build a
compiled form that collapses your search.  The compiled form of a word takes
the letters of a word and sorts them.   Use the compiled form as a
dictionary key to the original word (or words).  For example:
'bar' and 'bra' both compile to 'abr'.  Thus

words = {'arb': ('bar', bra')}

When you want to check a word, compile it by sorting its letters and test
its key against your words dictionary.

    a=list('bar')
    a.sort()
    compiled_word = string.join(a, '')
    if words.has_key(compiled_word):
        print words[compiled_word]

This would be blazingly fast, but would depend upon compiling your word
list.  There are lots of pretty complete text lists of English words
available on the web.

Andrew Henshaw



"dsavitsk" <dsavitsk at e-coli.net> wrote in message
news:gQzg6.67860$Ch.12128127 at newsrump.sjc.telocity.net...
> Thank you all for the suggestions.
>
> i am trying to unscramble scrambled words (which i am frustratingly bad
at)
> so i send all of the permutations to Word's spell checker and see what it
> thinks are words.  I'll post the code this afternoon when i'm done in case
> anyone wants it.
>
> doug
>
> "dsavitsk" <dsavitsk at e-coli.net> wrote in message
> news:tmlg6.61202$Ch.11526305 at newsrump.sjc.telocity.net...
> > i would like to iterate through all possible combinations of the
> characters
> > in a string, and i am not thinking very clearly about how to approach
> this.
> > that is, if the initial string is 'bar', i want to print
> > ['bar', 'bra', 'abr', 'arb', 'rba', 'rab']
> > any hints?
> >
> > thanks,
> > ds
> >
> >
>
>





More information about the Python-list mailing list