-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.py
34 lines (31 loc) · 900 Bytes
/
list.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
# what if we wanted to use recursion?
def palindrome_1(str):
if len(str) <= 1:
return True
if str[0] != str[-1]:
return False
return palindrome_1(str[1:-1])
# what if we didn't care about case?
def palindrome_2(str):
lower = str.lower()
for i in range(len(str) // 2):
if lower[i] != lower[-i - 1]:
return False
return True
# what if we wanted to ignore punctuation?
def palindrome_3(str):
punctuation = [',', '!', '?', '.']
no_punc_str = str[:]
for punc in punctuation:
no_punc_str = no_punc_str.replace(punc, '')
for i in range(len(no_punc_str) // 2):
if no_punc_str[i] != no_punc_str[-i - 1]:
return False
return True
# what if we wanted to ignore space?
def palindrome_4(str):
no_space_str = str.replace(' ', '')
for i in range(len(no_space_str) // 2):
if no_space_str[i] != no_space_str[-i - 1]:
return False
return True