Re: Finding matching words in a word game - Mailing list pgsql-general

From Alexander Farber
Subject Re: Finding matching words in a word game
Date
Msg-id CAADeyWgVQY=-jRCwwrXoArDTDf=uj5K8hj9KRVjZW2qnEybXbg@mail.gmail.com
Whole thread Raw
In response to Re: Finding matching words in a word game  (Chris Angelico <rosuav@gmail.com>)
List pgsql-general
Thanks, will try that (the "dumb" approach) too :-)

Still working on my dictionary (will be auto-generated by a script).


On Wed, Mar 6, 2013 at 2:57 PM, Chris Angelico <rosuav@gmail.com> wrote:
> words = {}
> for word in dictionary:   # provide a dictionary somehow - maybe from a file/db
>     words.setdefault(''.join(sorted(word)),[]).append(word)
> # Voila! You now have your mapping. One-off initialization complete.
>
> find_anagrams_of = "stop"
> anagrams = words.get(''.join(sorted(find_anagrams_of)),[])
> # anagrams is now a list of all known anagrams of the target -
> possibly an empty list
> print(anagrams)
>
>
> ['opts', 'post', 'pots', 'spot', 'stop', 'tops']
>
> On my laptop, loading ~100K words took about 1 second, and the lookup
> took effectively no time. I don't think there's any need for a heavy
> database engine here, unless you're working with millions and millions
> of words :)
>
> Chris Angelico
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general


pgsql-general by date:

Previous
From: Chris Angelico
Date:
Subject: Re: Finding matching words in a word game
Next
From: Florian Weimer
Date:
Subject: Re: [HACKERS] Floating point error