forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coding is_palindrome function and its unit test
- Loading branch information
Showing
2 changed files
with
100 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,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] |
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,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() |