From 2349a4b36fb6f3914d7e02dbb122ede4bce790d4 Mon Sep 17 00:00:00 2001 From: Kalyani Date: Sun, 16 Nov 2025 16:44:40 +0530 Subject: [PATCH 1/2] Enhance palindrome functions with type validation Refactor palindrome functions to include type checks and remove unnecessary comments. --- strings/palindrome.py | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/strings/palindrome.py b/strings/palindrome.py index e765207e5942..da0f4ec82a27 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -19,12 +19,8 @@ def is_palindrome(s: str) -> bool: - """ - Return True if s is a palindrome otherwise return False. - - >>> all(is_palindrome(key) is value for key, value in test_data.items()) - True - """ + if not isinstance(s, str): + return False start_i = 0 end_i = len(s) - 1 @@ -38,23 +34,16 @@ def is_palindrome(s: str) -> bool: def is_palindrome_traversal(s: str) -> bool: - """ - Return True if s is a palindrome otherwise return False. + if not isinstance(s, str): + return False + if len(s) < 2: + return True - >>> all(is_palindrome_traversal(key) is value for key, value in test_data.items()) - True - """ end = len(s) // 2 n = len(s) - # We need to traverse till half of the length of string - # as we can get access of the i'th last element from - # i'th index. - # eg: [0,1,2,3,4,5] => 4th index can be accessed - # with the help of 1st index (i==n-i-1) - # where n is length of string - return all(s[i] == s[n - i - 1] for i in range(end)) + return all(s[i] == s[n - i - 1] for i in range(end)) def is_palindrome_recursive(s: str) -> bool: """ From f72fa4d04627e575bd1afc37fe2eea9892675e00 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Nov 2025 11:15:32 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/palindrome.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/palindrome.py b/strings/palindrome.py index da0f4ec82a27..8e087dc72adb 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -42,9 +42,9 @@ def is_palindrome_traversal(s: str) -> bool: end = len(s) // 2 n = len(s) - return all(s[i] == s[n - i - 1] for i in range(end)) + def is_palindrome_recursive(s: str) -> bool: """ Return True if s is a palindrome otherwise return False.