From cbd6d5877e9a850a80160d9d15e6f9253ead5faf Mon Sep 17 00:00:00 2001 From: Ashwin Das Date: Fri, 22 May 2020 14:19:12 +0530 Subject: [PATCH 1/3] Update is_palindrome.py --- strings/is_palindrome.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/strings/is_palindrome.py b/strings/is_palindrome.py index 3070970ca6d0..8e665e6351b9 100644 --- a/strings/is_palindrome.py +++ b/strings/is_palindrome.py @@ -1,4 +1,4 @@ -def is_palindrome(s): +def is_palindrome(s: str) -> bool: """ Determine whether the string is palindrome :param s: @@ -7,8 +7,25 @@ def is_palindrome(s): True >>> is_palindrome("Hello") False + >>> is_palindrome("Able was I ere I saw Elba") + True + >>> is_palindrome("racecar") + True + >>> is_palindrome("Mr. Owl ate my metal worm?") + True """ - return s == s[::-1] + # Since Punctuation, capitalization, and spaces are usually ignored while checking Palindrome, + # we first remove them from our string. + cleaned_string = "" + for character in s: + if character.isalnum(): + cleaned_string += character + + # Palindromes are usually case insensitive. + # So we convert the string to lower case to make checking easier. + cleaned_string = cleaned_string.lower() + + return cleaned_string == cleaned_string[::-1] if __name__ == "__main__": From 705706626f2b244aad1870c2622f6d5722b3c2b4 Mon Sep 17 00:00:00 2001 From: Ashwin Das Date: Fri, 22 May 2020 15:04:28 +0530 Subject: [PATCH 2/3] Update is_palindrome.py --- strings/is_palindrome.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/strings/is_palindrome.py b/strings/is_palindrome.py index 8e665e6351b9..ec1673a670eb 100644 --- a/strings/is_palindrome.py +++ b/strings/is_palindrome.py @@ -16,10 +16,7 @@ def is_palindrome(s: str) -> bool: """ # Since Punctuation, capitalization, and spaces are usually ignored while checking Palindrome, # we first remove them from our string. - cleaned_string = "" - for character in s: - if character.isalnum(): - cleaned_string += character + cleaned_string = "".join([character for character in s if character.isalnum()]) # Palindromes are usually case insensitive. # So we convert the string to lower case to make checking easier. From 26dcf5a84b1319feb9d90889e5652c7d4526801c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 22 May 2020 11:49:00 +0200 Subject: [PATCH 3/3] Reuse s --- strings/is_palindrome.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/strings/is_palindrome.py b/strings/is_palindrome.py index ec1673a670eb..a0795b7b3783 100644 --- a/strings/is_palindrome.py +++ b/strings/is_palindrome.py @@ -16,13 +16,8 @@ def is_palindrome(s: str) -> bool: """ # Since Punctuation, capitalization, and spaces are usually ignored while checking Palindrome, # we first remove them from our string. - cleaned_string = "".join([character for character in s if character.isalnum()]) - - # Palindromes are usually case insensitive. - # So we convert the string to lower case to make checking easier. - cleaned_string = cleaned_string.lower() - - return cleaned_string == cleaned_string[::-1] + s = "".join([character for character in s.lower() if character.isalnum()]) + return s == s[::-1] if __name__ == "__main__":