Python Programming
Build a python programming that asks the user for a three-letter
substring. The
program should then proceed to read the file strings.txt. Each line
in strings.txt will contain a string. Your
program should then test to see how many non-overlapping
occurrences of the three-letter substring occur in
that string and test whether the string is a palindrome. The
program should then proceed to create an output
file string_stats.txt, which contains the original data on one line
and the number of substring occurrences and
palindrome test results on the next. After that, the file should
have one blank line, and then the output process
should repeat.
Input File is formatted such that each line is a string to be
tested. Leading and trailing whitespace for each line
must be deleted before processing.
Output format is:
Original_data_without_leading_or_trailing_whitespace + “\n” +
“Sub_String_Count:\t” + str(count) + “\t” +
is_or_not_Palindrome + “\n”
Note: Input files should not end with a newline. The last line in
the input file should not have a newline, as
having a new line will likely cause your program to check a blank
line or a line with just a newline.
Note: The program should handle FileNotFoundError
Required Design Scheme:
● Write a function isPalindrome(txt)
○ This function check if the string passed in is a palindrome
○ Returns True if it is a palindrome
○ Returns False if not
● Write a function getPalindrome(txt)
○ This function calls isPalindrome(txt) to check if the string is
palindrome
○ It returns "Is_Palindrome", when the string is a palindrome
○ Returns "Not_Palindrome", when the string is not a
palindrome
● Write a function readFile(file_name, target)
○ Takes in the file_name and three-letter substring
○ Opens the file
○ Creates the output_data list
○ Close File
○ Return output_data
● Write a function write_results(file_name, output_data)
○ Takes in the output file’s name, and the output data
○ Writes the data out to the file
● Write a function main()
○ Calls the above functions and any other helper functions as
needed
○ Asks the user for the Three-Letter substring
Sample Console Output:
>>>
Three Letter Substring to Find: llo
>>>
Sample Output File:
Hello
Sub_String_Count: 1 Not_Palindrome
Hello World
Sub_String_Count: 1 Not_Palindrome
Hello Hello Hello
Sub_String_Count: 3 Not_Palindrome
racecar
Sub_String_Count: 0 Is_Palindrome
111111
Sub_String_Count: 0 Is_Palindrome
222 222
Sub_String_Count: 0 Is_Palindrome
llll
Sub_String_Count: 0 Is_Palindrome
New World
Sub_String_Count: 0 Not_Palindrome
llooll
Sub_String_Count: 1 Is_Palindrome
llolloolloll
Sub_String_Count: 3 Is_Palindrome
Sample Input File:
Hello
Hello World
Hello Hello Hello
racecar
111111
222 222
llll
New World
llooll
llolloolloll
PROGRAM
import re
#checking for palindrom
def isPalindrome(line):
rev=line[::-1]
if rev==line:
return True
else:
return False
# return true or false for palindrome
def getPalindrome(line):
r=isPalindrome(line)
if r==True:
return
'Is_Palindrome'
else:
return
'Not_Palindrome'
#reading the input file and checking for substring and
palindrom
def readFile(filename,substring):
try:
output_data=[]
c=0
with open(filename,'r')
as f:
for line in f:
line=line.strip()
if line=='':
continue
for word in line.split():
l=re.findall(substring,word)
c+=len(l)
r=getPalindrome(line)
output_data.append(line)
output_data.append('Sub_String_Count: '+str(c)+' '+r +'\n')
c=0
f.close()
write_results('output.txt',output_data)
except FileNotFoundError as e:
print(e)
# writing result into a output file
def write_results(filename,output_data):
try:
with open(filename,'w')
as f:
for line in output_data:
f.write(line+'\n')
f.close()
except FileNotFoundError as e:
print(e)
if __name__=="__main__":
# getting input from the user 3 letter
substring
substring=input('Three Letter Substring to Find:
')
#calling the function
readFile('input.txt',substring)
SAMPLE OUTPUT:
C:\Users\abc\Desktop>python palin.py
Three Letter Substring to Find: llo
CONTENTS OF OUTPUT.TXT
Hello
Sub_String_Count: 1 Not_Palindrome
Hello World
Sub_String_Count: 1 Not_Palindrome
Hello Hello Hello
Sub_String_Count: 3 Not_Palindrome
racecar
Sub_String_Count: 0 Is_Palindrome
111111
Sub_String_Count: 0 Is_Palindrome
222 222
Sub_String_Count: 0 Is_Palindrome
llll
Sub_String_Count: 0 Is_Palindrome
New World
Sub_String_Count: 0 Not_Palindrome
llooll
Sub_String_Count: 1 Is_Palindrome
llolloolloll
Sub_String_Count: 3 Is_Palindrome
Get Answers For Free
Most questions answered within 1 hours.