Skip to content

Commit

Permalink
Merge pull request #57 from MIT-Emerging-Talent/are_anagrams
Browse files Browse the repository at this point in the history
are_anagrams
  • Loading branch information
theabdallahnjr authored Jan 11, 2025
2 parents 483e9d5 + 966d642 commit f4913e3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
45 changes: 45 additions & 0 deletions solutions/are_anagrams.py
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)
51 changes: 51 additions & 0 deletions solutions/tests/test_are_anagrams.py
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)

0 comments on commit f4913e3

Please sign in to comment.