fix: _remove_punctuation strips digits causing false palindrome results#2749
Merged
Merged
Conversation
Updated palindrome functions to consider only alphanumeric characters and ignore case. Improved docstrings for clarity.
keon
approved these changes
May 18, 2026
keon
left a comment
Owner
There was a problem hiding this comment.
APPROVED. Verified locally: test_string.py 68/68 pass. Good catch — replacing the ascii_letters membership test with str.isalnum() matches the behavior of the standalone is_palindrome function (which already used .isalnum()), so all five variants now agree. The left < right bound-guards on the inner while-loops in is_palindrome are a nice incidental hardening against all-punctuation input.
Note for the maintainer: this overlaps with the is_palindrome portion of #2745.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_remove_punctuation()inalgorithms/string/is_palindrome.pyusedascii_letters(a-z, A-Z only) to filter characters, silently discarding all digit characters (0-9).Four of the five palindrome-check variants —
is_palindrome_reverse,is_palindrome_two_pointer,is_palindrome_stack, andis_palindrome_deque— all call this helper, so they all returned wrong results for strings containing digits:Fix
Replace the
ascii_lettersmembership test in_remove_punctuationwithstr.isalnum(), which correctly retains digit characters:Remove the now-unused
from string import ascii_lettersimport. Update all five functions' docstrings to say "alphanumeric" (was "alphabetic"). Add bound-guardsleft < rightto the two inner while-loops inis_palindrometo prevent an infinite loop on all-punctuation input.Tests
Added
test_is_palindrome_with_digitswhich exercises all five variants with:"12321"→True(valid digit palindrome)"a1b2a"→False(not a palindrome once digits are kept)All 567 existing tests continue to pass.