A couple of lines of Python for a night of family fun

March 9, 2021

Have you ever wondered what words you can spell out using wooden cubes that have letters on each face? I have.

During my latest visit to my mother, I found some old wooden blocks with letters on each face on the kitchen table. I knew that these were hers from her childhood, but I was not sure what she had been using them for now. As it turns out, they can be used for a number of things: they can be put under her laptop to improve air flow and cooling, or they can be used to prop the kitchen window open to prevent a gust of wind slamming it shut.

During our conversation, we started playing around with the blocks. One of us suggested that we should try to find all the words we can spell out using the letters on them.

Okay, it was probably me who suggested this.

Programming > thinking hard

After we found a couple of words, I started losing interest. After all, how can we be sure we found all of them unless we brute force all the options? That would be approximately 6³ · 3!=1296 potential words, which is too many for my liking. If there are duplicate letters on the cubes, there are fewer options.

Programming is sometimes easier than thinking, so I wrote a script to find all the sensible words can be made using the letters on these cubes. Using the script, it was a more interesting game: I became the host and let my mother know how many undiscovered words she still had to find. When she got stuck, I could even help out a bit.

An algorithm for the cubes

The algorithm to find the words on the cubes is actually quite simple:

  1. Get a list of words from the dictionary.
  2. If the words have as many letters as cubes, check if (for any permutation of the cubes) we find each letter on one of the cubes. If so, we can use the cubes to spell out the words so we have a match. If the word is too short or too long, don’t even bother checking it.
  3. List out all the matching words we found.

The only challenging part is getting a list of valid words. Luckily, this can be done via the Dictionaries repository of LibreOffice, an open source office suite. If you are interested in the actual code of the algorithm, check out my GitHub repository for the project.

Admittedly, since there is only a not too high number of words in the dictionary, I did not bother optimizing the code too much — it runs very fast with three cubes and scales reasonably for more cubes or more than six letters per “cube”. Pull requests for optimization are welcome!

Making it accessible

Having code on GitHub is fantastic, however, not many people will actually bother with downloading and running the code. That’s why I decided to set up my project as a simple API, hosted on Heroku. Please note that the code is running on a free instance on Heroku, which gets shut down after a period of inactivity. If you are to first one to visit it in a while, you might have to wait a few seconds for the API to get going. If you submit another request, it should be much faster. The documentation of the API can be found here, provided by FastAPI on the Heroku deployment.

Using the extremely easy static website hosting provided by GitHub Pages, I also created a highly user-friendly front-end for the project. This latter one is the link I shared with my mother so that she can play around more with the cubes.

While I enjoyed this project quite a lot, writing code might not always be the best solution — it certainly has a fixed cost (mostly time). If, like me, you try to do it during a social event, people might just leave. In this case, I was lucky: my mother did not leave when I opened up my laptop to write the code for this project, and after tinkering for a couple of minutes, I was actually ready and we got going with the game. This ended up being a lot of fun and all matching words were found in the end.

Cover photo: Letter blocks. © the Author, 2021. Creative Commons CC BY-NC-SA 4.0

A couple of lines of Python for a night of family fun - March 9, 2021 - András Hann