Skip to content

Commit

Permalink
Fix special character handling and update test cases based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
emrebiyik committed Jan 6, 2025
1 parent 9795d22 commit a56cf01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 12 additions & 1 deletion solutions/is_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,23 @@ def is_palindrome(text: str) -> bool:
False
>>> is_palindrome("A man a plan a canal Panama")
True
>>> is_palindrome("$+$")
True
>>> is_palindrome("$+#")
False
"""
# Ensure the input is of type string to avoid unexpected errors.
assert isinstance(text, str), "Input must be a string"

# Normalize the text: remove spaces and convert to lowercase
normalized = "".join(char.lower() for char in text if char.isalnum())
normalized = "".join([char.lower() for char in text if not char.isspace()])

# Check if the string is the same when reversed
return normalized == normalized[::-1]


if __name__ == "__main__":
# Example cases to test functionality
print(is_palindrome("$+$")) # Should return True
print(is_palindrome("$+#")) # Should return False
print(is_palindrome("A man a plan a canal Panama")) # Should return True
27 changes: 27 additions & 0 deletions solutions/tests/test_is_palindrome.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit tests for the is_palindrome function.
This module contains test cases for the is_palindrome function,
which checks if a string is a palindrome. The tests cover:
- Empty strings
- Single-character strings
- Palindromes with lowercase, mixed-case, and spaces
- Palindromes containing numbers
- Palindromes containing special characters
- Non-palindromes
Created on 2024-12-30
Author: Emre Biyik
"""

import unittest
from solutions.is_palindrome import is_palindrome

Expand Down Expand Up @@ -53,6 +72,14 @@ def test_non_palindrome_with_special_characters_returns_false(self):
"""It should return False for non-palindromes with special characters."""
self.assertFalse(is_palindrome("hello!"))

def test_special_characters_palindrome_positive(self):
"""It should return True for special character palindrome."""
self.assertTrue(is_palindrome("$+$")) # Palindrome

def test_special_characters_palindrome_negative(self):
"""It should return False for non-palindrome special characters."""
self.assertFalse(is_palindrome("$+#")) # Non-palindrome


if __name__ == "__main__":
unittest.main()

0 comments on commit a56cf01

Please sign in to comment.