Last active
December 27, 2015 10:59
-
-
Save nifo/7315529 to your computer and use it in GitHub Desktop.
A way to get all anagrams from a file.
A python version of:
http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html and see multimaps.
This file contains hidden or 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
def anagrams(file_path, min_group_size=8): | |
"Gets all anagrams for the file at file_path. Print outs the result if " | |
"there are more than min_group_size (default 8) words." | |
"A python version of:" | |
"http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html and see multimaps." | |
m = {} | |
with open(file_path) as file: | |
for line in file: | |
word = line.strip() #removes the newline char | |
"A sorted word is the common key for all anagrams to that word." | |
"For example, 'aelst' is the key for both stale and steal." | |
key = str(sorted(word)) | |
m[key] = m.get(key, []) + [word] | |
for key in m: | |
if len(m[key]) >= min_group_size: | |
print "{length}: {words}".format(length= len(m[key]), words= m[key]) | |
anagrams() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment