Created
October 18, 2014 07:01
-
-
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.
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
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