This is how to quickly chew through a certain type of word puzzle that computers happen to be really good at. I have no idea what they are called, but someone will at least tell me the name if not the history. (Though maybe that history is apopocryptical, like Queen E the first inventing ascrotix.)
First, I am assuming you have an interactive python shell, such as when you start Idle normally. You type stuff and it replies stuff. Type 2+2, hit enter, see 4. But python (at least python 2) is not a good calculator. Type 7/2, press enter, see sadness assuming you are using python 2. This tutorial should work on either python2 or python3.
I'm also going to assume you have a dictionary/word list somewhere on your computer. I'll just use the standard one that comes with linux. Otherwise, download one.
from itertools import permutations words = set(line.strip().lower() for line in open('/usr/share/dict/words')) trips = 'AMI BLO BST COG CRE ERE NAC OFL OUR QTH STU SZE YME'.lower().split() maybe = set(''.join(p)[0:7] for p in permutations(trips, 3)) maybe & words
So, what is going on? There are not very many lines, but they are really high density. We are looking for a seven letter word made from three of the triples.
Permutations is a great tool for puzzles. You give it a list, and it returns all the permutations of length whatever. We use this later to permute all the three-length variations of the provided clues.
'/usr/share/dict/words' is where linux keeps the english dictionary. Substitute accordingly for you OS of choice.
line.strip().lower() first strips the newline off of the end of the line, the converts it to lowercase. I know you guys like doing everything in uppercase, so feel free to change all instances of
set(.....) takes whatever is inside and converts it to a set. Sets are really awesome and we will use it for a cool trick later.
words = set(line.strip().lower() for line in open('/usr/share/dict/words'))
is a list comprehension, which means a for loop squished into one line. The net effect is we have the entire english language stored in the variable
We start with a string of the triplets.
lower() converts them to lower case, and
split() chops it apart at each space. The string is now a list of three letter strings and stored in the
for p in permutatations(...)
goes through each 3-perm one at a time, assigning each 3-perm to the variable 'p'
''.join(p)[0:7] for p in ...
does two things.
takes the list-of-three-letter-strings and merges them together into one long string.
takes the first seven letters of that string. Later, we used
to get the 2nd through 8th letters, still seven long. (Yay zero indexing!)
The net result is to have every single possible seven letter word that could be made, to be stored in the variable
Here is where the sets shine. That ampersand? Set intersection! In a fraction of a second, all those nonsense permutations are compared against the entire english language. The matches are printed to your screen.