-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.py
46 lines (42 loc) · 1.19 KB
/
palindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import re
# def is_palindrome(sentence):
# '''uses iteration to strip non alphanum
# also performs a simple test for palindromes'''
# pal = []
# for x in sentence:
# if x.isalnum() == True:
# pal.append(x.lower())
# else:
# pass
# if pal[:] == pal[-1::-1]:
# return True
# else:
# return False
def is_palindrome(sentence):
''' uses recursive methods to check for palindromes'''
thing = re.sub(r'[^A-Za-z]',"",sentence).lower()
if len(thing) <=1:
return True
else:
if thing[0] == thing[-1]:
return is_palindrome(thing[1:-1])
else:
return False
# def is_palindrome(sentence):
# '''uses iterative methods to check for palindromes'''
# thing = re.sub(r'[^A-Za-z]',"",sentence).lower()
# count = 0 #count does double duty here
# for x in thing:
# if x == thing[-1-count]:
# count += 1
# else:
# pass
# if count == len(thing):
# return True
# else:
# return False
def main():
name = input("Please enter a word or sentence > ")
print(is_palindrome(name))
if __name__ == '__main__':
main()