Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
A script to find words in which every pair of adjacent characters
appears at least twice, by going through the Unix word list.
This was written for a Stack Exchange question:
def pairs(word):
"""Yield successive pairs of consecutive letters from the word."""
for i in xrange(len(word) - 1):
yield word[i:i+2]
with open('/usr/share/dict/words') as f:
for idx, line in enumerate(f):
word = line.strip().lower()
if len(word) <= 2:
for pair in pairs(word):
if word.count(pair) < 2:
print line.strip()

This comment has been minimized.

Copy link

ConorOBrien-Foxx commented Jul 6, 2015

Perhaps this will be simpler?:

def test(w):
    if len(w)>1:
        l = []
        for i in range(1,len(w)):
        l2 = set(l)
        return len(l)/len(l2)==2
    return False

Used as: test(word)? I wrote this for the same question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.