Skip to content

Instantly share code, notes, and snippets.

@shoark7
Last active August 31, 2019 04:46
Show Gist options
  • Select an option

  • Save shoark7/33a0980c65e47c02d5aacf8fc0e91069 to your computer and use it in GitHub Desktop.

Select an option

Save shoark7/33a0980c65e47c02d5aacf8fc0e91069 to your computer and use it in GitHub Desktop.
Introduce several ways to judge whether an word is a parlindrome.
# 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