is_anagram: bonus 1 & 2.

This commit is contained in:
Cacahuete 2021-01-31 17:33:55 +01:00
parent c04931e3a8
commit 516b2ad64c
2 changed files with 15 additions and 3 deletions

View file

@ -7,4 +7,16 @@ def is_anagram_v1(string1, string2):
return letters1 == letters2 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

View file

@ -25,7 +25,7 @@ class IsAnagramTests(unittest.TestCase):
self.assertTrue(is_anagram("Listen", "silent")) self.assertTrue(is_anagram("Listen", "silent"))
# To test the Bonus part of this exercise, comment out the following line # To test the Bonus part of this exercise, comment out the following line
@unittest.expectedFailure # @unittest.expectedFailure
def test_spaces_ignored(self): def test_spaces_ignored(self):
phrase1 = "William Shakespeare" phrase1 = "William Shakespeare"
phrase2 = "I am a weakish speller" phrase2 = "I am a weakish speller"
@ -33,7 +33,7 @@ class IsAnagramTests(unittest.TestCase):
self.assertFalse(is_anagram("a b c", "a b d")) self.assertFalse(is_anagram("a b c", "a b d"))
# To test the Bonus part of this exercise, comment out the following line # To test the Bonus part of this exercise, comment out the following line
@unittest.expectedFailure # @unittest.expectedFailure
def test_punctation_ignored(self): def test_punctation_ignored(self):
phrase1 = "A diet" phrase1 = "A diet"
phrase2 = "I'd eat" phrase2 = "I'd eat"