Question

Python Programming Build a python programming that asks the user for a three-letter substring. The program...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
Create program that sorts words of a string based on first letter of each word. Use...
Create program that sorts words of a string based on first letter of each word. Use C programming language. - Only use stdio.h and string.h - Remove any newline \n from input string - Use insert function - Input prompt should say "Enter string of your choice: " - Output should print sorted string on new line Example:     Enter string of your choice: this is a string     a is string this
Create a function in MIPS using MARS to determine whether a user input string is a...
Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully. Instructions: ●...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
Python programming Write a program that prompts the user to input the three coefficients a, b,...
Python programming Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equationax2+bx+c= 0.The program should display the solutions of this equation, in the following manner: 1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with4 digits printed out after the decimal place. 2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solutions, each displayed with4 digits printed...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Write a function that accepts a line of text and a single letter as input (case...
Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3...
Please program in JAVA: Program each option such as delete line and such. Your program will...
Please program in JAVA: Program each option such as delete line and such. Your program will be a line editor. A line editor is an editor where all operations are performed by entering commands at the command line. Commands include displaying lines, inserting text, editing lines, cutting and pasting text, loading and saving files. For example, a session where the user enters three lines of text and saves them as a new file may appear as: Menu: m Delete line:...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT