diff --git a/is_anagram/anagram.py b/is_anagram/anagram.py index 94e1c46..16c81b2 100644 --- a/is_anagram/anagram.py +++ b/is_anagram/anagram.py @@ -7,4 +7,16 @@ def is_anagram_v1(string1, string2): return letters1 == letters2 -is_anagram = is_anagram_v1 +def is_anagram_v2(string1, string2): + """Returns True if both strings are anagrams.""" + letters1 = { + l: string1.lower().count(l) for l in set(string1.lower()) if l.isalpha() + } + letters2 = { + l: string2.lower().count(l) for l in set(string2.lower()) if l.isalpha() + } + + return letters1 == letters2 + + +is_anagram = is_anagram_v2 diff --git a/is_anagram/test_is_anagram.py b/is_anagram/test_is_anagram.py index 662eecc..c5905fe 100644 --- a/is_anagram/test_is_anagram.py +++ b/is_anagram/test_is_anagram.py @@ -25,7 +25,7 @@ class IsAnagramTests(unittest.TestCase): self.assertTrue(is_anagram("Listen", "silent")) # To test the Bonus part of this exercise, comment out the following line - @unittest.expectedFailure + # @unittest.expectedFailure def test_spaces_ignored(self): phrase1 = "William Shakespeare" phrase2 = "I am a weakish speller" @@ -33,7 +33,7 @@ class IsAnagramTests(unittest.TestCase): self.assertFalse(is_anagram("a b c", "a b d")) # To test the Bonus part of this exercise, comment out the following line - @unittest.expectedFailure + # @unittest.expectedFailure def test_punctation_ignored(self): phrase1 = "A diet" phrase2 = "I'd eat"