mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Update is_palindrome.py (#2025)
* Update is_palindrome.py * Update is_palindrome.py * Reuse s Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
a15f82579d
commit
d8a4faf96d
@ -1,4 +1,4 @@
|
|||||||
def is_palindrome(s):
|
def is_palindrome(s: str) -> bool:
|
||||||
"""
|
"""
|
||||||
Determine whether the string is palindrome
|
Determine whether the string is palindrome
|
||||||
:param s:
|
:param s:
|
||||||
@ -7,7 +7,16 @@ def is_palindrome(s):
|
|||||||
True
|
True
|
||||||
>>> is_palindrome("Hello")
|
>>> is_palindrome("Hello")
|
||||||
False
|
False
|
||||||
|
>>> is_palindrome("Able was I ere I saw Elba")
|
||||||
|
True
|
||||||
|
>>> is_palindrome("racecar")
|
||||||
|
True
|
||||||
|
>>> is_palindrome("Mr. Owl ate my metal worm?")
|
||||||
|
True
|
||||||
"""
|
"""
|
||||||
|
# Since Punctuation, capitalization, and spaces are usually ignored while checking Palindrome,
|
||||||
|
# we first remove them from our string.
|
||||||
|
s = "".join([character for character in s.lower() if character.isalnum()])
|
||||||
return s == s[::-1]
|
return s == s[::-1]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user