Last active
July 16, 2021 13:34
-
-
Save minte9/1352435b5cc9eae11666813997431a10 to your computer and use it in GitHub Desktop.
Python / Language / Palindrome - https://www.minte9.com/python/math-development-1257
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a function called is_palindrom | |
# You can use built-in funciton len to check the string length | |
# A palindorm is a word that is spelled the same backward and forward | |
# Example: noon, redivider | |
def first(word): return word[0] | |
def last(word): return word[-1] | |
def middle(word): return word[1:-1] | |
# SOLUTION | |
def is_palindrome(word): | |
if (first(word) != last(word)): | |
return False | |
if (len(word) > 2): | |
return is_palindrome(middle(word)) | |
return True | |
print(is_palindrome("noon")) # True | |
print(is_palindrome("redivider")) # True | |
print(is_palindrome("to")) # False | |
print(is_palindrome("moomm")) # False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment