mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-22 21:57:35 +00:00
My favorite palindrome (#7455)
* My favorite palindrome * updating DIRECTORY.md * Update is_palindrome.py * Update is_palindrome.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update strings/is_palindrome.py Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com> * Update is_palindrome.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
This commit is contained in:
parent
0dc95c0a6b
commit
b8b63469ef
@ -1,9 +1,8 @@
|
|||||||
def is_palindrome(s: str) -> bool:
|
def is_palindrome(s: str) -> bool:
|
||||||
"""
|
"""
|
||||||
Determine whether the string is palindrome
|
Determine if the string s is a palindrome.
|
||||||
:param s:
|
|
||||||
:return: Boolean
|
>>> is_palindrome("A man, A plan, A canal -- Panama!")
|
||||||
>>> is_palindrome("a man a plan a canal panama".replace(" ", ""))
|
|
||||||
True
|
True
|
||||||
>>> is_palindrome("Hello")
|
>>> is_palindrome("Hello")
|
||||||
False
|
False
|
||||||
@ -14,15 +13,15 @@ def is_palindrome(s: str) -> bool:
|
|||||||
>>> is_palindrome("Mr. Owl ate my metal worm?")
|
>>> is_palindrome("Mr. Owl ate my metal worm?")
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
# Since Punctuation, capitalization, and spaces are usually ignored while checking
|
# Since punctuation, capitalization, and spaces are often ignored while checking
|
||||||
# Palindrome, we first remove them from our string.
|
# palindromes, we first remove them from our string.
|
||||||
s = "".join([character for character in s.lower() if character.isalnum()])
|
s = "".join(character for character in s.lower() if character.isalnum())
|
||||||
return s == s[::-1]
|
return s == s[::-1]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
s = input("Enter string to determine whether its palindrome or not: ").strip()
|
s = input("Please enter a string to see if it is a palindrome: ")
|
||||||
if is_palindrome(s):
|
if is_palindrome(s):
|
||||||
print("Given string is palindrome")
|
print(f"'{s}' is a palindrome.")
|
||||||
else:
|
else:
|
||||||
print("Given string is not palindrome")
|
print(f"'{s}' is not a palindrome.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user