Skip to content

Instantly share code, notes, and snippets.

@Ostapp
Created February 14, 2016 20:25
Show Gist options
  • Save Ostapp/341d335528ebb2c1db16 to your computer and use it in GitHub Desktop.
Save Ostapp/341d335528ebb2c1db16 to your computer and use it in GitHub Desktop.
import time
def is_anagram(word1, word2):
# sorted converts to list by characters and sorts
return sorted(word1) == sorted(word2)
def reduce_list(mylist, reduce_factor):
new_list = list()
for i, word in enumerate(mylist):
if i % reduce_factor == 0:
new_list.append(word)
# for i in range(len(mylist)):
# word = mylist[i]
# if i % reduce_factor == 0:
# new_list.append(word)
return new_list
def reduce_list_elegant(mylist, reduce_factor):
return mylist[::reduce_factor]
def anagram_list_list(words):
list_of_groups = list()
for word in words:
found = False
for anagram_group in list_of_groups:
if is_anagram(word, anagram_group[0]):
anagram_group.append(word)
found = True
break
if not found:
list_of_groups.append([word])
[
['abcd', 'dcba'],
['xyzw']
]
with open('words.txt') as f:
words_src = f.read().lower().split()
reduce_factor = 50
words = reduce_list_elegant(words_src, reduce_factor)
start = time.time()
anagram_groups = anagram_list_list(words)
end = time.time()
print 'total seconds: {}'.format(end - start)
print 'number of elements: {}'.format(len(words))
import time
def make_anagram_groups(word_list):
anagram_groups = {}
for word in word_list:
key = ''.join(sorted(word))
if key not in anagram_groups:
anagram_groups[key] = [word]
else:
anagram_groups[key].append(word)
return anagram_groups
data = {
'abc': {'bca', 'cab', 'cba'},
'def': {'fed', 'edf', 'def'}
}
reduce_factor = 1
with open('words.txt') as f:
words_src = f.read().lower().split()
word_list = set(words_src[::reduce_factor])
start = time.time()
anagram_groups = make_anagram_groups(word_list)
end = time.time()
print 'total seconds: {}'.format(end - start)
print 'number of elements: {}'.format(len(word_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment