Find how to get from one word to another through a “ladder” of words that each differ by one letter.
Run the code to get a list of dictionary words that start with “z” and have four letters. Get words that have other patterns of letters (e.g. start with “a”, then have four letters):
Get a list of all dictionary words in English:
Get a list of words that match the pattern of starting with “z” and then having 3 more letters:
Find the five words that are “nearest” to “elephant”. Try out different words and see which other words are “nearest” (e.g. try your name):
Note: upper and lower case are considered different.
This finds 5 words nearest to “elephant”, based on changing the fewest letters:
Find all the words that differ from “fish” by one letter. Try out different words to find words that differ from them by one letter (e.g. try your name):
Note: longer words may have no nearby other words.
This finds all the words in the dictionary that differ from “fish” by at most 1 letter:
Use Rest to keep the “rest” of the list, dropping the original word:
Make a network. Try making networks using different numbers than 2, 1, and 10:
We’re going to want to create networks of words. Let’s start by creating a network of numbers.
This defines a particular way to connect numbers from 0 to 9:
Graph draws a network or “graph”; VertexLabels->“Name” says to label vertices with their names:
Make a network of words that differ by one letter. Try making networks for different classes of words (e.g. words beginning with “w” instead of “q”):
Note: this will take a long time if your class of words is big.
This creates a list of words that are going to be in the network, here consisting of “q” followed by 3 letters:
We’re naming this list words.
Now we construct a function that finds nearest words:
We can apply this function to any word in the list, saying we want all words that differ by at most 1 letter:
Use Rest to keep only the “rest” of the list, dropping the first element (which is here the original word):
To find the whole network, we need to apply this to every word in the list:
The # and & set up a pure function, which is then mapped over the original list of words.
To form the actual network, we need to “thread” these connections. Thread turns a connection to a list into a list of connections—for example:
Thread the nearest connections:
Then flatten out all the sublists:
Now we can turn this into a graph, or network:
Run the first expression to set up a network of words with four letters. Then try some different words with four letters in the second expression (e.g. “pigs” or “nose”) to find word ladders between them:
Note: the words need to be in the dictionary.
This creates a network (named g) of nearest words for all 4-letter dictionary words (note that there’s a semicolon at the end to avoid printing the very big result):
This finds the shortest path on the network from “fish” to “fowl”—a word ladder:
Run this after you’ve run the previous expression: