This notebook was prepared by Donne Martin. Source and license info is on GitHub.
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
from collections import OrderedDict
class Anagram(object):
def group_anagrams(self, items):
# TODO: Implement me
pass
The following unit test is expected to fail until you solve the challenge.
# %load test_anagrams.py
import unittest
class TestAnagrams(unittest.TestCase):
def test_group_anagrams(self):
anagram = Anagram()
self.assertRaises(TypeError, anagram.group_anagrams, None)
data = ['ram', 'act', 'arm', 'bat', 'cat', 'tab']
expected = ['ram', 'arm', 'act', 'cat', 'bat', 'tab']
self.assertEqual(anagram.group_anagrams(data), expected)
print('Success: test_group_anagrams')
def main():
test = TestAnagrams()
test.test_group_anagrams()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.