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:
@@ -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])
|
||||||
|
|||||||
Reference in New Issue
Block a user