Now with formatting

Original Now with formatting Editable
version 1 of 1

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.)

Kyle (2011-11-06-17-17-46-821)

It was Queen Victoria.

Kyle (2013-01-07-20-44-32-319)

This sort of trigram word puzzle is called an Anaquote.

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.

So, code time. (The greater than sign is the prompt where you type.)

from itertools import permutations
words = set(line.strip().lower() for line in open('/usr/share/dict/words'))
maybe = set(''.join(p)[0:7] for p in permutations(trips, 3))
maybe & words

You get "cognacs" as the output. Now, what if we wanted to skip the first letter?

maybe = set(''.join(p)[1:8] for p in permutations(trips, 3))
maybe & words

Loamier, lobster, stamina, thereof.

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.

from itertools import permutations

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.

words = set(line.strip().lower() for line in open('/usr/share/dict/words'))

'/usr/share/dict/words' is where linux keeps the english dictionary. Substitute accordingly for you OS of choice.

open('path to file') loads in the text file.

... for line in open(...) loops through the text file one line at a time.

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 .lower() to .upper().

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.

Altogether, 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 words.


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 trips variable.

maybe = set(''.join(p)[0:7] for p in permutations(trips, 3))

permutations(trips, 3) makes every single 3-permunations.

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. ''.join(p) takes the list-of-three-letter-strings and merges them together into one long string. [0:7] takes the first seven letters of that string. Later, we used [1:8] 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 maybe.

Also, these are packed into another set just with the dictionary of words.

maybe & words

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.

.... and I need some sort of clincher or conclusion here. But pretend it has an awesome one.

jarav (2011-02-16-03-39-48-957)

Good intro to using permutations in itertools.

Have come across your awk music on reddit. Cool stuff.

Please do come up with more articles/tutorials.

A mention of India in one of your articles(LCD Hacks). Have been there?

Am an Arch Linux user too.