Skip to content

Instantly share code, notes, and snippets.

@alexwlchan
Created July 6, 2015 20:03
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 alexwlchan/973a270729f77c4acdaa to your computer and use it in GitHub Desktop.
Save alexwlchan/973a270729f77c4acdaa to your computer and use it in GitHub Desktop.
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
Copy link

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