Newspaper Word Games

Chandrasekhar Gudipati
4 min readMay 12, 2021
Photo by Abo Ngalonkulu on Unsplash

Picture this. It’s a warm Sunday evening and you are sipping your cappuccino while glancing through the day’s headlines. As you turn the pages over, you come across the Puzzles segment. You put your cappuccino down, get a pen, rip your hair out, tear the paper up and walk away frustrated. Believe me, I know this feeling all too well. To get around this problem after a particularly frustrating encounter with the back page, I wrote a few Python codes that figure out the solutions directly. In this article, I’ve added two codes, one for the Spellathon and the other for Word Scramble. If you just want the Markdown files for the code, visit my Github repo at https://github.com/cgudipati/Newspaper-Word-Games. Let’s delve into the code explanation, shall we?

SPELLATHON

Spellathon

I guess the instructions are pretty straightforward in this image. So here’s an outline of what our code does. When you type in the first letter followed by the auxiliary letters, you’ll get all possible answers for the puzzle. So here, the input would be “rlawdor”. Let’s try to execute this code directly.

So what did I do here? Firstly, please try to understand the itertools package. It’s great for jumbling stuff up in any order possible. There are many great and easy-to-understand resources available online. What I did first was to split the user input string into two parts: one for the main letter and the other for the auxiliary letters. I then opened a line-separated text file that had all words in the English dictionary. After this, I used itertools to create different combinations of the code, ranging in length from 4 to 7. I then simply compared each of these combinations with those from the dictionary and printed the words out whenever there was a match.

But wait, if you’ve gone through the .ipynb file in the Github link, you might’ve noticed a repetition of certain words. That’s because letters repeat in the puzzle. For example, there are two occurrences of the letter ‘R’. Our code would take words with a single ‘R’ twice since it considers each case as an unrelated scenario. To avoid this showing up in the answer, I created a new list and added a new word to that list only when there was no occurrence of that word already in the list. This gave us the perfect solution.

SCRAMBLE

Scramble

This is another common puzzle that I managed to write a code for. It’s actually a much simpler code than the one for Spellathon, believe it or not! The primary reason for this is the fact that the length of the word here remains fixed unlike in the case of Spellathon. The user input here would directly be the word itself. The output would be a list of all possible correct words (what’s to say that there is only one solution?). Generally, it’s just one word.

This code uses itertools again. There really isn’t much explanation required here. All I did was jumble up the letters to all possible combinations and then simply match them to the dictionary file. The repeating words problem reveals itself here as well, but luckily for us, I figured out the solution for that in the previous code. All I had to do was to copy that code snippet and paste it here.

NOTE

Some words might not match up with the solutions to your particular puzzle, simply because we might be using different dictionaries. I’ve added a provision for a few other dictionaries both in the executable code and Github. Feel free to replace corncob_lowercase.txt in the part of the code that is highlighted below with either words.txt or wordlist.txt and see what changes. If you happen to find a more appropriate dictionary for your puzzles, just add that dictionary file to the working directory of the code and have at it.

f = open(r”corncob_lowercase.txt”, “r”)

As far as the license is concerned, please do not hesitate to use any part of my code for any part of your projects. All I ask in return is that you share any improvements that you find with everyone. Let’s together foster a sense of a community of coders!

I will be adding codes for more puzzles and games in the future, so please subscribe and stay tuned for more information. I will probably start with crossword puzzles and then move up the complexity ladder.

I hope you enjoyed this little hack to solve your word puzzles. Always try them manually first though, they are a lot of fun!

--

--

Chandrasekhar Gudipati

An avid tech enthusiast with a keen interest in Data Science, IOT and Graphic Design.