Code Switch: Anagrammatist | Jonathan Perry-Houts
Jonathan Perry-Houts

Code Switch: Anagrammatist

I thought I’d write some brief blog posts about software I’ve written in my spare time. This first one is about a piece of code I wrote back in college to help generate anagrams for unsecured marquee signs. I recently rewrote this in Javascript, and it should work in most modern browsers. You can find it here, or use the embedded widget below.

000 000 000 000 000 000 000 000 00

Finding anagrams by hand is fun, and not that hard, but I thought it’d be an interesting challenge to come up with an algorithm that filters a dictionary of tens of thousands of words down to a list of words that are spellable using some arbitrary set of available letters.

The algorithm I came up with converts each word into a 26-index array of integers, representing the quantity of each letter (A-Z) required to spell the word. This can be done quickly for every word in a dictionary, and stored efficiently in memory.

More importantly, though, finding out which words in the dictionary can be spelled with some arbitrary set of letters is as simple as subtracting each word from the available letters. If the result contains any negative numbers we can’t spell that word. All spellable words are put in a text box, and updated automatically when the user enters a new letter in either the input or anagram box. The resulting program is pretty fun to use!

As a last touch, the program also recognizes that some letters can be substituted for other letters (W’s for M’s for example). This feature is kind of a hack. It just counts ‘Z’ as ‘N’ and ‘W’ as ‘M’ in all instances. This works just fine, but results in some weird status messages. For example, if you try to use more W’s than there are available, the message you get tells you that you’re short on M’s. That functionality hasn’t been added to the javascript version yet, but would be trivial to add and extend if I had the time.

The code is on github. You can run the Python version locally by following the file instructions.