diff --git a/.vscode/settings.json b/.vscode/settings.json index bbda5188d..252022b48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -119,8 +119,8 @@ "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.ruff": true, - "source.organizeImports.ruff": true + "source.fixAll.ruff": "explicit", + "source.organizeImports.ruff": "explicit" } } } diff --git a/solutions/README.md b/solutions/README.md index 9852346d2..420ed1e86 100644 --- a/solutions/README.md +++ b/solutions/README.md @@ -1 +1,21 @@ # Solutions + +## Palindrome Checker + +This module checks if a given string is a palindrome. A palindrome reads the same backward as forward, ignoring case and spaces. + +### How to Use + +1. Run the `is_palindrome.py` file to use the function. +2. Use `test_is_palindrome.py` to test the functionality. + +#### Examples + +```python +>>> from is_palindrome import is_palindrome +>>> is_palindrome("level") +True +>>> is_palindrome("hello") +False +>>> is_palindrome("A man a plan a canal Panama") +True diff --git a/solutions/__init__.py b/solutions/__init__.py index 8b1378917..e69de29bb 100644 --- a/solutions/__init__.py +++ b/solutions/__init__.py @@ -1 +0,0 @@ - diff --git a/solutions/is_palindrome.py b/solutions/is_palindrome.py new file mode 100644 index 000000000..b55b46ea3 --- /dev/null +++ b/solutions/is_palindrome.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for checking if a string is a palindrome. + +Module contents: + - is_palindrome: Checks if a given string is a palindrome. + +Created on 2024-12-30 +Author: Emre Biyik +""" + + +def is_palindrome(text: str) -> bool: + """Checks if a string is a palindrome. + + A palindrome is a word, phrase, or sequence that reads the same backward as forward, ignoring case and spaces. + + Parameters: + text: str, the input string to check. + + Returns -> bool: True if the input is a palindrome, False otherwise. + + Raises: + AssertionError: if input is not a string. + + Examples: + >>> is_palindrome("level") + True + >>> is_palindrome("hello") + 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 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 diff --git a/solutions/tests/__init__.py b/solutions/tests/__init__.py index 8b1378917..e69de29bb 100644 --- a/solutions/tests/__init__.py +++ b/solutions/tests/__init__.py @@ -1 +0,0 @@ - diff --git a/solutions/tests/test_is_palindrome.py b/solutions/tests/test_is_palindrome.py new file mode 100644 index 000000000..7a4a562ae --- /dev/null +++ b/solutions/tests/test_is_palindrome.py @@ -0,0 +1,85 @@ +#!/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 + + +class TestIsPalindrome(unittest.TestCase): + """Test the is_palindrome function.""" + + def test_empty_string(self): + """It should return True for an empty string.""" + self.assertTrue(is_palindrome("")) + + def test_single_character(self): + """It should return True for single character strings.""" + self.assertTrue(is_palindrome("a")) + + def test_level_palindrome(self): + """It should return True for the palindrome 'level'.""" + self.assertTrue(is_palindrome("level")) + + def test_radar_palindrome(self): + """It should return True for the palindrome 'radar'.""" + self.assertTrue(is_palindrome("radar")) + + def test_hello_is_not_palindrome(self): + """It should return False for the non-palindrome 'hello'.""" + self.assertFalse(is_palindrome("hello")) + + def test_world_is_not_palindrome(self): + """It should return False for the non-palindrome 'world'.""" + self.assertFalse(is_palindrome("world")) + + def test_mixed_case_palindromes_return_true(self): + """It should return True for palindromes with mixed lower-case and upper-case characters.""" + self.assertTrue(is_palindrome("RaceCar")) + + def test_palindromes_with_spaces_return_true(self): + """It should return True for palindromes with spaces and mixed cases.""" + self.assertTrue(is_palindrome("A man a plan a canal Panama")) + + def test_palindrome_with_numbers_returns_true(self): + """It should return True for palindromes containing numbers.""" + self.assertTrue(is_palindrome("12321")) + + def test_non_palindrome_numbers_return_false(self): + """It should return False for non-palindrome numbers.""" + self.assertFalse(is_palindrome("12345")) + + def test_palindrome_with_special_characters_returns_true(self): + """It should return True for palindromes with special characters.""" + self.assertTrue(is_palindrome("!@#$%^&*()_+_)(*&^%$#@!")) + + 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()