-
-
Save alexwlchan/973a270729f77c4acdaa to your computer and use it in GitHub Desktop.
paired_characters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps this will be simpler?:
Used as:
test(word)
? I wrote this for the same question.