Skip to content

Instantly share code, notes, and snippets.

@bennuttall
Last active January 5, 2020 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennuttall/6ef5f5278aa686dd9d7b8ba0eee9cedb to your computer and use it in GitHub Desktop.
Save bennuttall/6ef5f5278aa686dd9d7b8ba0eee9cedb to your computer and use it in GitHub Desktop.

word chain challenge

Given a wordlist (say, the English dictionary), if you can remove a letter from a word and the new word is a known word (in the wordlist), this makes a chain of length 2. If you can repeat this for a second time, you continue to the third level, and so on.

For example, the word "trees" has a chain length of 3:

Using the system dictionary (code snippet provided), how long is the longest chain? And which words make the longest chain?

Get started

from words import WORDS
alphabet = 'qwertyuiopasdfghjklzxcvbnm'
with open('/usr/share/dict/british-english') as f:
WORDS = {
word.strip()
for word in f
if all(c in alphabet for c in word.strip())
}
print(len(WORDS), 'words')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment