Menu

Sign in to track your progress and unlock all features.

Theme style

Start learning freeLog in

Problem No 23

Check Palindrome with Loop

Medium

18 minute session

Summary

Determine whether a string is a palindrome using explicit loops and ignoring non-alphanumeric characters.

Problem statement

Write is_palindrome(s) that returns True if the string s is a palindrome when considering only alphanumeric characters and ignoring case, otherwise return False. Use loops (for or while) to compare characters; do not use slicing like s[::-1] or the reversed() built-in for the main comparison logic. Examples of treatment: - 'A man, a plan, a canal: Panama' -> True - 'race a car' -> False - Empty string or strings with no alphanumeric characters are palindromes (return True).

Task

Implement a function that checks if a string reads the same forwards and backwards, ignoring case and non-alphanumeric characters, using loops (no slicing for reversal).

Examples

Classic palindrome phrase

Input

is_palindrome('A man, a plan, a canal: Panama')

Output

True

Explanation

Ignoring non-alphanumeric characters and case, the string becomes 'amanaplanacanalpanama', which is the same forwards and backwards.

Input format

A single argument: s (string).

Output format

Boolean True if palindrome according to the rules, otherwise False.

Constraints

String length <= 1000. Use explicit loops for comparison; you may use str.isalnum() and str.lower().

Samples

Sample input 0

is_palindrome('Racecar')

Sample output 0

True

Explanation 0

Case-insensitive palindrome.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.