diff --git a/solutions/is_palindrome.py b/solutions/is_palindrome.py new file mode 100644 index 000000000..797da018d --- /dev/null +++ b/solutions/is_palindrome.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for checking if a given string is a palindrome (reads the same backward as forward) + +Module contents: + - palindrome: input string is palindrome if it reads the same backward as forward + +Created on XX XX XX +@author: Saad M. Ashour +""" + + +def is_palindrome(s: str) -> bool: + """ + Check if a given string is a palindrome. + + A palindrome is a word, phrase, number, or other sequence of characters + that reads the same forward and backward, ignoring spaces, punctuation, + and case sensitivity. + + Parameters: + s (str): The input string to be checked. + + Returns: + bool: True if the input string is a palindrome, False otherwise. + + Examples: + >>> is_palindrome("A man, a plan, a canal, Panama") + True + >>> is_palindrome("hello") + False + >>> is_palindrome("Madam") + True + """ + # Remove non-alphanumeric characters and convert to lowercase + cleaned = "".join(char.lower() for char in s if char.isalnum()) + + # Check if the cleaned string is equal to its reverse + return cleaned == cleaned[::-1] diff --git a/solutions/tests/test_is_palindrome.py b/solutions/tests/test_is_palindrome.py new file mode 100644 index 000000000..f6d746a61 --- /dev/null +++ b/solutions/tests/test_is_palindrome.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for is_palindrome function. + +@author: Saad M. Ashour +""" + +import unittest + +from ..is_palindrome import is_palindrome + + +class TestIsPalindrome(unittest.TestCase): + """ + Unit tests for the is_palindrome function. + + These tests check various cases to ensure that the is_palindrome function correctly + identifies palindromes, considering case insensitivity, non-alphanumeric characters, + and other edge cases. + """ + + def test_palindrome(self): + """ + Test cases where the string is a palindrome. + """ + self.assertTrue(is_palindrome("A man, a plan, a canal, Panama")) + self.assertTrue(is_palindrome("racecar")) + self.assertTrue(is_palindrome("Madam")) + + def test_non_palindrome(self): + """ + Test cases where the string is not a palindrome. + """ + self.assertFalse(is_palindrome("hello")) + self.assertFalse(is_palindrome("world")) + + def test_empty_string(self): + """ + Test case where the input is an empty string, which is considered a palindrome. + """ + self.assertTrue(is_palindrome("")) + + def test_single_character(self): + """ + Test case where the input is a single character, which is always a palindrome. + """ + self.assertTrue(is_palindrome("a")) + self.assertTrue(is_palindrome("1")) + + def test_only_non_alphanumeric(self): + """ + Test case where the input contains only non-alphanumeric characters. + The function should ignore them and return True for empty string. + """ + self.assertTrue(is_palindrome("!@#$$@!")) + + +if __name__ == "__main__": + unittest.main()