Created
June 17, 2016 22:22
-
-
Save Zaargh/d600e06a7b2e6186bc32555b56440a10 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
import unittest | |
from collections import Counter | |
def find_anagrams(list_of_strings, word): | |
return [c for c in list_of_strings | |
if Counter(c.lower()) == Counter(word.lower()) | |
if c.lower() != word.lower()] | |
class AnagramsTestCase(unittest.TestCase): | |
def test_example(self): | |
self.assertEqual( | |
find_anagrams(["spare", "hello", "pears", "world", "reaps"], | |
"parse"), | |
["spare", "pears", "reaps"] | |
) | |
def test_case_insensitive(self): | |
self.assertEqual( | |
find_anagrams(["Spare", "hello", "pears", "world", "reaps"], | |
"parse"), | |
["Spare", "pears", "reaps"] | |
) | |
def test_same_word_isnt_anagram(self): | |
self.assertEqual( | |
find_anagrams(['strawberry'], 'strawberry'), | |
[] | |
) | |
self.assertEqual( | |
find_anagrams(['ok', 'Ok', 'OK'], 'ok'), | |
[] | |
) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment