-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathproblem_17.py
More file actions
30 lines (28 loc) · 972 Bytes
/
problem_17.py
File metadata and controls
30 lines (28 loc) · 972 Bytes
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
'''
Write a version of a palindrome recognizer that
also accepts phrase palindromes such as "Go hang a
salami I'm a lasagna hog.", "Was it a rat I
saw?", "Step on no pets", "Sit on a potato pan,
Otis", "Lisa Bonet ate no basil",
"Satan, oscillate my metallic sonatas", "I roamed under
it as a tired nude Maori", "Rise to vote sir", or the
exclamation "Dammit, I'm mad!". Note that
punctuation, capitalization, and spacing are usually ignored
'''
def palindrome(string):
string = string.lower() #only string.lower won't work
string = string.replace(" ","") #must be contained in
tem = ['.',',','!','?','\"',"\'"] #variable :)
for i in tem:
string = string.replace(i,"")
tmp=''
length = len(string)
for i in range(length):
tmp += string[length-1]
length -= 1
if string == tmp:
return True
else:
return False
ab = "Was it a rat I saw?"
print (palindrome(ab))