Created
March 28, 2015 11:19
-
-
Save patrickt/5c47b37737af619d0915 to your computer and use it in GitHub Desktop.
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
def abbrevify(word): | |
assert(len(word) >= 4) | |
num = len(word) - 2 | |
return word[0] + str(num) + word[-1] | |
results = {} | |
for word in open('/usr/share/dict/words'): | |
word = word.strip() | |
if len(word) >= 4 and word.islower(): | |
abbrevd = abbrevify(word) | |
given = results.get(abbrevd, []) | |
results[abbrevd] = given + [word] | |
filtered = dict((key,value[0]) for key, value in results.iteritems() if len(value) == 1) | |
print len(filtered), "words can be unambiguously abbreviated" | |
for key, value in filtered.iteritems(): | |
print value, "==>", key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment