Skip to main content

Command Palette

Search for a command to run...

Problem 5: Palindrome Checker

Updated
2 min read
Problem 5: Palindrome Checker

Hey everyone! 👋

Continuing with our coding challenges series! Today we're looking at a classic string manipulation problem: checking for palindromes.

The Problem

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

Examples:

  • "racecar" -> True

  • "A man a plan a canal Panama" -> True

  • "hello" -> False

The Solution

Here is a clean Python solution that handles case insensitivity and ignores spaces:

def is_palindrome(s):
    """
    Checks if a string is a palindrome.
    Ignores case and spaces.
    """
    s = str.lower(s)
    s = s.replace(" ", "")
    return s == s[::-1]

# Test cases
print(is_palindrome("wilabaliw"))
# Output: True
print(is_palindrome("A man a plan a canal Panama"))
# Output: True

Code Breakdown

Let's break down how this works:

  1. s = str.lower(s)

    • First, we convert the entire string to lowercase. This ensures that "Racecar" and "racecar" are treated as the same string.
  2. s = s.replace(" ", "")

    • Next, we remove all spaces. This is important for phrases like "A man a plan..." where the spaces don't match up when reversed, but the letters do.
  3. return s == s[::-1]

    • This is the Pythonic magic! s[::-1] creates a reversed copy of the string.

    • We simply compare the cleaned original string with its reverse. If they are equal, it returns True; otherwise, False.

Short, sweet, and effective!


Stay tuned for more challenges! Happy coding! 💻

Learning how to code with AI

Part 10 of 15

In this series, I’ll solve AI-generated Python challenges — from pseudo code to final solution — to rebuild my problem-solving skills, sharpen logic, and show how AI can help us learn to think like real developers again.

Up next

Problem 4: Flatten a Nested List

Hey everyone! 👋 I know I've been a bit quiet lately. I actually came down with a pretty bad flu last week, which completely knocked me out. 🤒 That's why I missed posting about the coding challenges. I'm finally feeling a bit better and ready to get...

More from this blog

F

Freelance Full-Stack Developer | Django + React | Shopify, WordPress & Automation | I Build Web Experiences That Convert

92 posts

Freelance Full-Stack Developer | Django + React | Shopify, WordPress & Automation | I Build Web Experiences That Convert