forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from MIT-Emerging-Talent/are_anagrams
are_anagrams
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for checking whether two strings are anagrams. | ||
Module contents: | ||
- are_anagrams: takes 2 strings as arguments and check whether they are anagrams. | ||
Created on 03 01 25 | ||
@author: Abdallah Alnajjar | ||
""" | ||
|
||
|
||
def are_anagrams(word_1: str, word_2: str) -> bool: | ||
""" | ||
Checks whether two strings are anagrams. | ||
Parameters: | ||
word_1 (str): The first word (string) to be compared. | ||
word_2 (str): The second word (string) to be compared. | ||
Returns: | ||
bool: "True" if the two string are anagrams, | ||
and "False" if it's not the case | ||
Raises: | ||
AssertionError: If the one of the inputs is not a string. | ||
>>> are_anagrams("Lemon", "melon") | ||
True | ||
>>> are_anagrams("Here come dots", "The Morse Code") | ||
True | ||
>>> are_anagrams("Hello", "Yellow") | ||
False | ||
""" | ||
assert isinstance(word_1, str) | ||
assert isinstance(word_2, str) | ||
|
||
# Remove spaces and convert to lowercase for accurate comparison | ||
word_1 = word_1.replace(" ", "").lower() | ||
word_2 = word_2.replace(" ", "").lower() | ||
|
||
# Check if sorted characters of both strings are equal | ||
return sorted(word_1) == sorted(word_2) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
A module for testing the are_anagrams function. | ||
Tests included: | ||
- When two words are anagarms | ||
- When two words are not anagrams | ||
- When the input is two phrases, not words | ||
- When the input is not a string | ||
Created on 04 01 24 | ||
@author: Abdallah Alnajjar | ||
""" | ||
|
||
import unittest | ||
|
||
from ..are_anagrams import are_anagrams | ||
|
||
|
||
class TestAnagrams(unittest.TestCase): | ||
""" | ||
Tests the function "are_anagrams" in the file anagarms. | ||
""" | ||
|
||
def test_anagrams_two_correct_words(self): | ||
""" | ||
It should return True if Earth and Heart are anagrams | ||
""" | ||
actual = are_anagrams("Earth ", "Heart") | ||
self.assertEqual(actual, True) | ||
|
||
def test_anagrams_two_incorrect_words(self): | ||
""" | ||
It should return False since David and Aboodi are not anagrams | ||
""" | ||
actual = are_anagrams("David", "Aboodi") | ||
self.assertEqual(actual, False) | ||
|
||
def test_anagrams_phrases(self): | ||
""" | ||
It should return True as the two phrases are anagrams | ||
""" | ||
actual = are_anagrams("I am not active", "Vacation time") | ||
self.assertEqual(actual, True) | ||
|
||
def test_anagrams_integer(self): | ||
""" | ||
It should raise an assertion error if one of the inputs | ||
is not a string | ||
""" | ||
with self.assertRaises(AssertionError): | ||
are_anagrams(20105140, 4351315) |