Coding Challenge: Palindrome Checker
Test your Python skills with this interactive challenge. Build a palindrome checker with step-by-step solutions and bonus challenges.
A palindrome is a word or phrase that reads the same forwards and backwards. In this challenge, you'll build a program to check if text is a palindrome. This exercise teaches string manipulation, a fundamental programming skill.
The Challenge
Write a Python function that checks if a word or phrase is a palindrome. The function should ignore spaces, punctuation, and be case-insensitive.
Examples:
is_palindrome("radar") → True
is_palindrome("hello") → False
is_palindrome("A man a plan a canal Panama") → True
Rules
Ignore spaces and punctuation. Make it case-insensitive (A == a). Return True or False. Write in Python 3.
Starter Code
def is_palindrome(text):
# Your code here
pass
# Test cases
print(is_palindrome("radar")) # Should print: True
print(is_palindrome("hello")) # Should print: False
Try solving it yourself before looking at the solution below. The hints might help if you get stuck.
Hints
Click for hints (try without them first!)
Hint 1: Python can reverse strings with string[::-1]
Hint 2: Remove spaces with string.replace(" ", "")
Hint 3: Make lowercase with string.lower()
Hint 4: For phrases, remove all non-letters first using re.sub()
Solution
Click to reveal solution
Basic Solution
def is_palindrome(text):
# Convert to lowercase
text = text.lower()
# Remove spaces
text = text.replace(" ", "")
# Check if equal to reverse
return text == text[::-1]
# Test
print(is_palindrome("radar")) # True
print(is_palindrome("hello")) # False
print(is_palindrome("Race car")) # True
Advanced Solution (handles punctuation)
import re
def is_palindrome(text):
# Convert to lowercase
text = text.lower()
# Remove all non-alphanumeric characters
text = re.sub(r'[^a-z0-9]', '', text)
# Check if equal to reverse
return text == text[::-1]
# Test with complex examples
print(is_palindrome("A man, a plan, a canal: Panama")) # True
print(is_palindrome("Was it a car or a cat I saw?")) # True
How It Works
The [::-1] trick: This is Python's slice notation for reversing. The syntax is [start:end:step], and -1 means go backwards.
Breaking it down: First, convert to lowercase so "Race" and "race" match. Then remove spaces so "race car" becomes "racecar". Finally, reverse it and compare—if they match, it's a palindrome.
Bonus Challenges
Challenge 1: Count palindromes in a list. Given words = ["radar", "hello", "level", "world", "civic"], count how many are palindromes.
Challenge 2: Find the longest palindrome in a string. Given "abacdfgdcaba", find the longest palindromic substring.
Challenge 3: Create a palindrome generator. Turn any word into a palindrome.
Bonus challenge solutions
Challenge 1 Solution
def count_palindromes(word_list):
return sum(1 for word in word_list if is_palindrome(word))
words = ["radar", "hello", "level", "world", "civic"]
print(count_palindromes(words)) # 3
Challenge 2 Solution
def longest_palindrome(text):
longest = ""
for i in range(len(text)):
for j in range(i + 1, len(text) + 1):
substring = text[i:j]
if is_palindrome(substring) and len(substring) > len(longest):
longest = substring
return longest
print(longest_palindrome("abacdfgdcaba")) # "aba"
Challenge 3 Solution
def make_palindrome(text):
text = text.lower()
return text + text[::-1][1:]
print(make_palindrome("cat")) # "catac"
Palindrome checking is used in data validation (serial numbers, credit cards), competitive programming, DNA sequence analysis, and pattern recognition.
Share your solution! Post your code in the comments. Bonus points for creative approaches or optimizations.