Hackbright Code Challenges

Most Common Anagram

Most Common Anagram

Challenge

Medium

Concepts

General

Download

anagrams.zip

Solution

Most Common Anagram: Solution


An anagram is a word that is a valid rescrambling of another word. For example, ‘cat’ is an anagram of ‘act’.

The file words.txt contains a list of 4-to-6 letter words. Find the word that has the most anagrams of any other word in that list (since any anagram of that word would have the same number of anagrams, return the first, alphbetically)

For example:

>>> all_words = [w.strip() for w in open('words.txt')]
>>> find_most_anagrams_from_wordlist(all_words)
'angor'

We’ve provided a Python file, anagram.py, but the implemented isn’t complete, so the tests fail.

anagram.py
def find_most_anagrams_from_wordlist(wordlist):
    """Given list of words, return the word with the most anagrams."""

Hint

First Step

You might find it useful to first make a function that turns a list of words into a dictionary that maps those letters to the matching words, like this:

def make_anagram_dict(words):
    """Return dict mapping sorted letters -> [words w/ those letters]

        >>> make_anagram_dict(["act", "cat", "dog", "god"])
        {'dgo': ['dog', 'god'], 'act': ['act', 'cat']}
    """