From the course: Python Data Analysis

Overview: Finding anagrams - Python Tutorial

From the course: Python Data Analysis

Overview: Finding anagrams

- [Instructor] In chapter one, we have reviewed Python loops, data containers, and comprehensions. We will get some practice with these elements of the core language in a simple, practical project, finding anagrams in the English dictionary. As you know, two words are anagrams of each other when their letters can be rearranged to turn one word into the other. For instance, stop can be anagrammed into post, spot, tops, pots, and opts. We'll use this simple strategy. We define the signature of a word as the sorted list of its letters including duplicates. So the signature of Python would be hnopty. Two words are anagrams of each other if they have the same signature. Thus, we are going to make a Python dict of all the words in a dictionary indexed by the signature. Looking up if a word has an anagram would then be as simple as computing its signature and looking it up in the dict. Let's begin.

Contents