๐งช Programming
โฑ๏ธ 31s
๐ 1532 chars
๐ฆ 1.5B
๐ง Q4_K_M
Prompt: Write a Python function is_palindrome(s) that returns True if s is a palindrome. One test case.
Response
To check whether a string `s` is a palindrome (reads the same forwards and
backwards), you can use an algorithm like this:
```python
def is_palindrome(s):
"""
Check if the provided string s is a palindrome.
Args:
- s: A string
Returns:
- True if s is a palindrome, False otherwise.
"""
# Normalize by converting to lowercase and removing non-alphanumeric ch
characters
normalized_s = ''.join(filter(str.isalnum, s.lower()))
left, right = 0, len(normalized_s) - 1
while left < right:
if normalized_s[left] != normalized_s[right]:
return False
left += 1
right -= 1
return True
# Test case
print(is_palindrome("Racecar")) # Should print: True
```
### Explanation:
- **Normalization**:
- Converts the string to lowercase to ignore case differences.
- Removes all non-alphanumeric characters by using a filter that checks i
if each character is alphanumeric.
- **Palindrome Check Algorithm**:
- Initializes two pointers, `left` and `right`, at the start and end of t
the normalized string respectively.
- Compares the characters at these two positions. If they are not equal,
it returns `False`.
- Moves the `left` pointer one step forward and the `right` pointer one s
step backward until all characters have been checked.
### Test Case:
The function should correctly identify "Racecar" as a palindrome since it r
reads the same forwards and backwards when case is ignored.