Fix is_palindrome_recursive logic in strings/palindrome.py (#12946)

* Fix: is_palindrome_recursive logic for 2-char strings

* Update palindrome.py

* Update palindrome.py

* Update palindrome.py

---------

Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
hema_ameh
2025-09-04 06:19:59 +05:30
committed by GitHub
parent 0876a87186
commit 544f48ff34

View File

@@ -11,6 +11,8 @@ test_data = {
"BB": True, "BB": True,
"ABC": False, "ABC": False,
"amanaplanacanalpanama": True, # "a man a plan a canal panama" "amanaplanacanalpanama": True, # "a man a plan a canal panama"
"abcdba": False,
"AB": False,
} }
# Ensure our test data is valid # Ensure our test data is valid
assert all((key == key[::-1]) is value for key, value in test_data.items()) assert all((key == key[::-1]) is value for key, value in test_data.items())
@@ -61,7 +63,7 @@ def is_palindrome_recursive(s: str) -> bool:
>>> all(is_palindrome_recursive(key) is value for key, value in test_data.items()) >>> all(is_palindrome_recursive(key) is value for key, value in test_data.items())
True True
""" """
if len(s) <= 2: if len(s) <= 1:
return True return True
if s[0] == s[len(s) - 1]: if s[0] == s[len(s) - 1]:
return is_palindrome_recursive(s[1:-1]) return is_palindrome_recursive(s[1:-1])