Skip to content

Commit

Permalink
Merge branch 'master' of github.com:epequeno/ThinkPy-Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
epequeno committed Dec 4, 2016
2 parents b8778ae + ef97936 commit 62abfae
Show file tree
Hide file tree
Showing 9 changed files with 1,288 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
*.pyc
.solutions
venv2
.ThinkPy-Solutions
.ipynb_checkpoints
36 changes: 20 additions & 16 deletions ch12/12.04.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,29 @@

# Status: Complete

from collections import defaultdict

with open('words.txt', 'r') as fd:
words = fd.read().splitlines()


def make_anagram_dict(word_list):
'''Take a list of words, return a dict with a fingerprint as the key
and the anagrams made from that fingerprint as the value.'''
anagrams = dict()
"""Take a list of words, return a dict with a fingerprint as the key
and the anagrams made from that fingerprint as the value."""
result = defaultdict(list)
for word in word_list:
fp = ''.join(sorted(word))
anagrams[fp] = anagrams.get(fp, [])
anagrams[fp].append(word)
result[fp].append(word)

result = {fp: result[fp] for fp in result if len(result[fp]) > 1}
return result

anagrams = {fp: anagrams[fp] for fp in anagrams if len(anagrams[fp]) > 1}
return anagrams

anagrams = make_anagram_dict(words)


def print_anagrams(anagrams):
'''Uses a generator to call and print 5 items from mydict'''
"""Uses a generator to call and print 5 items from mydict"""
fp = (fp for fp in anagrams)

print "Sample from anagram dict:"
Expand All @@ -58,8 +62,8 @@ def print_anagrams(anagrams):


def sort_anagrams(anagrams):
'''Returns a list of lists containing all anagram matches. The longest list
(most anagrams) is at the top'''
"""Returns a list of lists containing all anagram matches. The longest list
(most anagrams) is at the top"""
anagrams_lists = []
for fp in anagrams:
anagrams_lists.append(anagrams[fp])
Expand All @@ -76,8 +80,8 @@ def sort_anagrams(anagrams):


def find_bingos(anagrams):
'''Filters mydict for keys of length 8. Sorts a list of the values
(lists) and sorts by length in reverse order'''
"""Filters mydict for keys of length 8. Sorts a list of the values
(lists) and sorts by length in reverse order"""
candidates = [anagrams[key] for key in anagrams if len(key) == 8]
candidates.sort(key=len, reverse=True)

Expand All @@ -93,8 +97,8 @@ def find_bingos(anagrams):


def is_metathesis(reference, test):
'''If two anagrams mismatch exactly twice they are metathesis pairs.
Caution: This function assumes strings of equal length'''
"""If two anagrams mismatch exactly twice they are metathesis pairs.
Caution: This function assumes strings of equal length"""
i = 0
count = 0
while i <= (len(reference) - 1):
Expand All @@ -107,8 +111,8 @@ def is_metathesis(reference, test):


def find_metathesis(anagrams):
'''mydict values are lists, we use index 0 as a reference and check the
rest of the list (1 to end of list) against that reference word.'''
"""mydict values are lists, we use index 0 as a reference and check the
rest of the list (1 to end of list) against that reference word."""
answer = []
for fp in anagrams:
reference = anagrams[fp][0]
Expand Down
Loading

0 comments on commit 62abfae

Please sign in to comment.