Skip to content

Commit

Permalink
is_palindrome function
Browse files Browse the repository at this point in the history
coding is_palindrome function and its unit test
  • Loading branch information
sashour82 committed Dec 31, 2024
1 parent 347d834 commit 33b19ba
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
40 changes: 40 additions & 0 deletions solutions/is_palindrome.py
Original file line number Diff line number Diff line change
@@ -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]
60 changes: 60 additions & 0 deletions solutions/tests/test_is_palindrome.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 33b19ba

Please sign in to comment.