Skip to content

Instantly share code, notes, and snippets.

@j605
Created December 9, 2011 13:49
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 j605/1451599 to your computer and use it in GitHub Desktop.
Save j605/1451599 to your computer and use it in GitHub Desktop.
#! /usr/bin/python2.7
def signature(s):
t = list(s)
t.sort()
t = ''.join(t)
return t
def all_anagrams(filename):
d = {}
for line in open(filename):
word = line.strip().lower()
t = signature(word)
if t not in d:
d[t] = [word]
else:
d[t].append(word)
return d
def list_anagrams(d):
l = d.values()
l.sort()
print l
def for_scrabble(d):
l = d.values()
ctr = []
for w in l:
if len(w[0]) == 8:
ctr.append(len(w))
maximum = max(ctr)
for w in l:
if len(w[0]) == 8 and len(w) == maximum:
print w
def count(s1,s2):
ctr = 0
for i in range(len(s1)):
if s1[i] != s2[i]:
ctr += 1
if ctr == 2:
return True
return False
def compare(words):
s = []
for s1 in words:
global s1
for s2 in words:
if count(s1,s2):
s.append([s1,s2])
return s
def meta_pair(d):
l = d.values()
for words in l:
if len(words) > 1:
print compare(words)
d = all_anagrams('words.txt')
#list_anagrams(d)
#for_scrabble(d)
meta_pair(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment