Last active
August 31, 2019 04:46
-
-
Save shoark7/33a0980c65e47c02d5aacf8fc0e91069 to your computer and use it in GitHub Desktop.
Introduce several ways to judge whether an word is a parlindrome.
This file contains hidden or 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
| # 1. basic: using for loop | |
| def is_palindrome_basic(word): | |
| SIZE = len(word) | |
| for i in range(SIZE // 2): | |
| if word[i] != word[SIZE-1-i]: | |
| return False | |
| return True | |
| # 2. Pythonic way: reversing word easily | |
| def is_palindrome_reverse(word): | |
| return word == word[::-1] | |
| # 3. recursion: function call | |
| def is_palindrome_recursive(word): | |
| def compare(lo, hi): | |
| if lo >= hi: | |
| return True | |
| if word[lo] == word[hi]: | |
| return compare(lo+1, hi-1) | |
| else: | |
| return False | |
| return compare(0, len(word)-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment