forked from arsho/46-Simple-Python-Exercises-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_32.py
More file actions
29 lines (26 loc) · 878 Bytes
/
problem_32.py
File metadata and controls
29 lines (26 loc) · 878 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
'''
Write a version of a palindrome recogniser
that accepts a file name from the
user, reads each line, and prints the line to the screen
if it is a palindrome
'''
def palindrome(string):
string = string.lower()
string = string.replace(" ","").replace("\n","") # lines contain '\n'
tem = ['.',',','!','?','\"',"\'"] # in file, need to remove
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
print("Enter your filename with extension : ")
file_name = input(">>")
with open(file_name,'r') as f:
for item in f.readlines(): # readlines() create list
print(palindrome(item)) # of all lines till EOF