Skip to content

Commit

Permalink
docs: Add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
teihenn committed Nov 15, 2024
1 parent a1ba775 commit 769e283
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/anagram.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
This module contains functions to determine if two strings are anagrams.
"""

from collections import defaultdict


Expand All @@ -16,9 +20,7 @@ def are_anagrams_sorting(str1, str2):

# Method 2: Using Character Count with defaultdict for both strings
def are_anagrams_dict(str1, str2):
"""
Determines if two strings are anagrams by counting characters with defaultdict for both strings.
"""
"""Determines if two strings are anagrams by counting characters with defaultdict for both strings."""
# Remove whitespace and convert to lowercase for case-insensitive comparison
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
Expand All @@ -42,8 +44,8 @@ def are_anagrams_dict(str1, str2):

# Method 3: Using Character Count with Array (assuming ASCII characters)
def are_anagrams_array(str1, str2):
"""
Determines if two strings are anagrams by counting characters with an array.
"""Determines if two strings are anagrams by counting characters with an array.
Assumes only ASCII characters.
"""
# Remove whitespace and convert to lowercase for case-insensitive comparison
Expand Down
8 changes: 8 additions & 0 deletions test/test_anagram.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""Test cases for the anagram module."""

import unittest

from src.anagram import are_anagrams_array, are_anagrams_dict, are_anagrams_sorting


class TestAnagramMethods(unittest.TestCase):
"""Test cases for the anagram module."""

def setUp(self):
"""Set up the test cases."""
self.test_cases = [
("listen", "silent", True),
("triangle", "integral", True),
Expand All @@ -16,6 +21,7 @@ def setUp(self):
]

def test_are_anagrams_sorting(self):
"""Test the are_anagrams_sorting function."""
for str1, str2, expected in self.test_cases:
with self.subTest(str1=str1, str2=str2):
self.assertEqual(
Expand All @@ -25,6 +31,7 @@ def test_are_anagrams_sorting(self):
)

def test_are_anagrams_dict(self):
"""Test the are_anagrams_dict function."""
for str1, str2, expected in self.test_cases:
with self.subTest(str1=str1, str2=str2):
self.assertEqual(
Expand All @@ -34,6 +41,7 @@ def test_are_anagrams_dict(self):
)

def test_are_anagrams_array(self):
"""Test the are_anagrams_array function."""
for str1, str2, expected in self.test_cases:
with self.subTest(str1=str1, str2=str2):
self.assertEqual(
Expand Down

0 comments on commit 769e283

Please sign in to comment.