Compute all palindromic substrings of length ≥ 7 of a textfile
Write a program that given a long string (say order 1.000.000 characters), computes all palindromic substrings of length ≥ 7, i.e. substrings spelled identical read forwards and backwards. You can use the below code to read a complete text from a file to a string, convert it to lower case, and remove everything except letters and digits. The file saxo.txt is a local copy of http://www.gutenberg.org/cache/epub/1150/pg1150.txt
from string import ascii_letters, digits
s = open("The Danish History.txt").read()
s = s.lower()
s = "".join([c for c in s if c in ascii_letters or c in digits])
def isPalindrome(str):
def isPal(str):
if len(str) >= 7:
return True
else:
str[0] == str[-1] and isPal(str[1:-1])
return isPal(s)
print(isPalindrome(s))
I got this this exercise where I have to compute all palindromic substrings of length ≥ 7 of a textfile. I have used the above codes to show that my string/textfile is a palindrome. I don't know what I have to do now to compute a list of all palindromic substrings of length ≥ 7.
Thanks in advance.
from Recent Questions - Stack Overflow https://ift.tt/39hQbIc
https://ift.tt/eA8V8J
Comments
Post a Comment