Skip to content

Commit

Permalink
Apply Ruff formatting to code
Browse files Browse the repository at this point in the history
  • Loading branch information
emrebiyik committed Dec 31, 2024
1 parent 17ee226 commit 042d2c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
17 changes: 9 additions & 8 deletions solutions/is_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
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
Expand All @@ -32,9 +33,9 @@ def is_palindrome(text: str) -> bool:
True
"""
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 char.isalnum())

# Check if the string is the same when reversed
return normalized == normalized[::-1]
18 changes: 9 additions & 9 deletions solutions/test_is_palindrome.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import unittest
from 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_simple_palindrome(self):
"""It should return True for simple palindromes."""
self.assertTrue(is_palindrome("level"))
self.assertTrue(is_palindrome("radar"))

def test_non_palindrome(self):
"""It should return False for non-palindromes."""
self.assertFalse(is_palindrome("hello"))
self.assertFalse(is_palindrome("world"))

def test_mixed_case_palindrome(self):
"""It should handle mixed case palindromes."""
self.assertTrue(is_palindrome("RaceCar"))

def test_palindrome_with_spaces(self):
"""It should handle palindromes with spaces."""
self.assertTrue(is_palindrome("A man a plan a canal Panama"))

def test_not_a_string(self):
"""It should raise AssertionError for non-string input."""
with self.assertRaises(AssertionError):
is_palindrome(12321)


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


0 comments on commit 042d2c9

Please sign in to comment.