Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created March 28, 2015 11:19
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 patrickt/5c47b37737af619d0915 to your computer and use it in GitHub Desktop.
Save patrickt/5c47b37737af619d0915 to your computer and use it in GitHub Desktop.
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