Skip to content

Instantly share code, notes, and snippets.

@dalanmiller
Created October 18, 2014 07:01
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 dalanmiller/c644e4e814d6a5f32b7e to your computer and use it in GitHub Desktop.
Save dalanmiller/c644e4e814d6a5f32b7e to your computer and use it in GitHub Desktop.
Quick script to use `/usr/share/dict/words` and count the amount words which can be inner-transposed to another word. Also prints the average length of words with alternates.
from collections import Counter
words = []
with open("/usr/share/dict/words","r") as f:
for line in f:
words.append(line.strip())
words_list = []
for w in words:
if len(w) > 3:
w = w.lower()
l = list(w[1:-1])
l.sort()
l = tuple(l)
words_list.append( (w[0], l, w[-1]) )
c = Counter(words_list)
s_vals = [ (k,v) for k,v in c.items() ]
s_vals = sorted(s_vals, key=operator.itemgetter(1), reverse=True )
#All the words with transposable alternates (inner anagrams?)
print s_vals
print "AVERAGE LENGTH", float(sum([ len(x[0][1]) + 2 for x in s_vals]))/len(s_vals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment