๐งช Programming
โฑ๏ธ 70s
๐ 1873 chars
๐ฆ 3.1B
๐ง Q4_K_M
Prompt: Write a Python function is_palindrome(s) that returns True if s is a palindrome. One test case.
Response
Certainly! Below is a Python function `is_palindrome` that checks whether a
a given string `s` is a palindrome. A palindrome is a word, phrase, number,
number, or other sequences of characters which reads the same backward as f
forward.
Here's the implementation with one test case to demonstrate its usage:
```python
def is_palindrome(s):
"""
Check if the input string s is a palindrome.
:param s: The string to check.
:return: True if s is a palindrome, False otherwise.
"""
# Normalize the string by removing non-alphanumeric characters and
converting to lowercase
normalized_s = ''.join(char.lower() for char in s if char.isalnum())
# Check if the normalized string reads the same forwards and backwards
return normalized_s == normalized_s[::-1]
# Test Case
test_string = "A man, a plan, a canal: Panama"
print(is_palindrome(test_string)) # Expected output: True
```
### Explanation:
- The function `is_palindrome` takes a string as input.
- It first normalizes the string by removing any non-alphanumeric
characters and converting all letters to lowercase. This step is optional b
but makes the function more robust in handling different inputs, such as pu
punctuation or case sensitivity issues.
- After normalization, it checks if the normalized string reads the same fo
forwards and backwards using slicing (`[::-1]`), which reverses the string.
string.
### Test Case:
For `"A man, a plan, a canal: Panama"`, removing non-alphanumeric
characters and converting to lowercase results in `"amanapalanacame"`. This
This is indeed a palindrome because it reads the same backward as forward.
Thus, `is_palindrome` returns `True` for this test case.
Feel free to use or modify this function based on your needs!