diff --git a/strings/palindrome.py b/strings/palindrome.py index e765207e5942..8e087dc72adb 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,21 +34,14 @@ 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))