
Introduction
I’m a shameless puzzle enthusiast, and my current obsession is the New York Times Spelling Bee. In this daily puzzle, players are given a ‘hive’ of seven letters. They’re tasked with creating words which may only contain those letters, and which must include the letter at the center of the hive (known as the ‘center letter’). Valid words must also be at least four letters long. In the imitation puzzle below, for instance, ‘accolade’ would be a valid word, but ‘load’ (no center letter) and ‘cap’ (too short) would not be.

The goal is to think of as many valid words as possible: as your word count goes up, so does your performance ranking, which lies on a scale from ‘Beginner’ to ‘Genius.’ I have lost an embarrassing amount of sleep some nights in pursuit of that prized Genius status.
Today, I decided to solve the puzzle by relying on Python instead of English, and this article will explain how I did it.
Isn’t this Cheating?
This task transformed a word puzzle into a logic puzzle, and in the sense that there was still some ‘solving’ to do, I don’t think of it as cheating. Given that the logic puzzle only needed to be solved once before it could be applied to any future word puzzle, however, I’d undoubtedly be cheating if I were to simply run the program on tomorrow’s Spelling Bee challenge.
That would take all the fun out of solving the Spelling Bee, so if I use this program in the future, it will only be after I’ve exhausted every valid word in my vocabulary and am still up at 2AM chasing the Genius rank. In the spirit of good sportsmanship, I would ask my readers to only use the information in this article for educational purposes.
Creating a List of English Words
A program for solving the Spelling Bee would need to have some way of recognizing whether or not a word exists in the English language. For that, I downloaded the ‘words.txt’ file from this GitHub repository. Then, I created a list of words in the file with the following code structure:
The words in ‘word_file’ contained unnecessary characters at the end, ‘/n’, denoting a new line. These characters were removed with string indexing, and the words were changed to lowercase for consistency, before being appended to ‘wordlist.’ The list contained about 25.5k words in total, which is far from exhaustive, but presumably includes enough words for our purposes.
Defining the Solver Function
The function below returns a list of potentially valid words, given a puzzle’s constraints, without importing any dependencies:
The function defines unacceptable letters as all letters in the alphabet that are not in the list of letters that it takes as an argument. The list of acceptable words is populated by iterating through ‘wordlist’ and identifying words that meet the following conditions:
- The center letter provided as an argument must be in the word.
- The word must not already be in the acceptable words list (to avoid duplicates appearing in the final list).
- The length of the word string must be greater than 3.
- The word must not contain any of the letters that are in the unacceptable letters list.
If all of these conditions are met, the word is appended to the acceptable words list. The function returns the list as output after iterating through ‘wordlist.’
Testing the Function
Now let’s test the function on a set of actual Spelling Bee puzzle constraints. Below, we define valid letters for a puzzle as a list of single-character strings, and the center letter as a stand-alone single-character string.
Given this input, the function returned the following list of words:
[‘alai’,’alan’,’alfalfa’,’alia’,’alicia’,’allan’,’annal’,’annual’,’annul’,’annuli’,’calculi’,’calf’,’call’,’calla’,’canal’,’cilia’,’clan’,’cliff’,’clinic’,’clinician’,’cull’,’facial’,’fail’,’fall’,’fanciful’,’faunal’,’filial’,’fill’,’final’,’financial’,’finial’,’flail’,’flan’,’fluff’,’fulfill’,’full’,’iliac’,"i’ll",’lacuna’,’lain’,’lana’,’liana’,’lila’,’lilac’,’lilian’,’lillian’,’lucia’,’lucian’,’lull,’lulu’,’nail’,’null’,’ucla’,’ulan’,’ulna’]
It should be noted that not all of the above words were valid entries for the Spelling Bee puzzle. Only relatively common words are accepted for most puzzles, and words containing punctuation are invalid. Obscure words and those that may be proper nouns, including ‘alicia,’ ‘ucla’, and ‘ulan’, were not accepted. This list nonetheless pushed my score for today’s puzzle above the ‘Genius’ threshold.
Try it yourself!
Want to test the function on another set of constraints? The full code for this solver, as well as the text file containing English words, can be found in this GitHub repository. Just remember: bee a good sport and try solving the puzzle on your own first.