Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
paired_characters.py
#!/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:
http://puzzling.stackexchange.com/q/17471/4692
"""
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:
continue
for pair in pairs(word):
if word.count(pair) < 2:
break
else:
print line.strip()
@ConorOBrien-Foxx

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)):
            p=w[i-1]+w[i]
            l+=[p]
        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.