An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
November 22, 2021 12:26 am GMT
Original Link: https://dev.to/mahmoudessam/python-challenge11-58d6
Python challenge_11
Palindrome
level of challenge 4/10
A string is a palindrome when it is the same
when read backwards.
For example:
the string "bob" is a palindrome So is "abba".
But the string "abcd" is not a palindrome, because "abcd" != "dcba".
Write a function named palindrome that takes a single string as its parameter.
Your function should return True if the string is a palindrome, and False otherwise.
My solution
def palindrome(string): pal_string = list( string.casefold() ) rev_string = list( reversed(pal_string) ) if pal_string == rev_string: return True else: return Falseprint(palindrome("aba"))
Another solution
def palindrome(string): while len(string) > 1: head = string[0] tail = string[-1] string = string[1:-1] if head != tail: return False return True
def palindrome(string): if len(string) < 2: return True return string[0] == string[-1] and palindrome(string[1:-1])
def palindrome(string): return string == string[::-1]
I hope this challenge improve python skills
Original Link: https://dev.to/mahmoudessam/python-challenge11-58d6
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To