Starting from:

$30

CSC401–Introduction to Programming 3 Solved

Your assignment is to solve the following problems (A & B) in Python. These 2 should be put in 2 python files (assgn3_a.py and assgn3_b.py), to be executed in the Python IDLE interface as previously covered.

Problem A)

1.       Create a function that accepts a file name and search string parameter and returns the number of times the search string appears in the file. The below instructs how this must be done.

2.       This function should load the specified text file (we will use the provided “Dracula.txt”) and count all instances of the specified search string found in that file. NOTE: You must use the line-by-line method of processing the text file for this problem. The count should be returned from the method.

3.       To call this function, prompt the user for the file name, then again prompt the user for the search string

(the search string should be alphabetic only). Then pass the file name and the search string to your function. Save the returned count in a variable.

4.       Print a result statement in this format:  SearchString appears in FileName ResultingCount times 

5.       Examples for you to test with:

                                          Enter Text File Name: Dracula.txt                Enter Text File Name: Dracula.txt 

                                         Enter Text to Search For: Whitby                  Enter Text to Search For: Borgo 

                                         Whitby appears in Dracula.txt 43 times           Borgo appears in Dracula.txt 11 times 

    

Enter Text File Name: Dracula.txt     Enter Text File Name: Dracula.txt Enter Text to Search For: Harker       Enter Text to Search For: Vampire 

Harker appears in Dracula.txt 160 times   Vampire appears in Dracula.txt 14 times  

6.       In your python file, be sure to define the function first, then add the user-interaction, function invocation, and output generation afterwards:

def your_function(file_name, search_string): 

your function code… your function code… your function code… 

 



# Here get the file name and the search string from the user # 

 



# Here call your function and save the resulting value # 

 



# Here print the results as specified:  Whitby appears in Dracula.txt 43 times # 
 

Problem B)

1.       Create a function that accepts a file name and search string parameter and returns extracts of the text that contain the search string. The below instructs how this must be done.

2.       This function should load the specified text file (we will use “Dracula.txt”) and then find all instances of the search string found in that file.  

For each of these instances, print an output statement showing the instance count (1 through n), and a string containing the 30 characters before the search string, the search string, and the 30 characters after the search string.  If there are any newline’s, replace them with the “|” character. For example:

he other two were Exeter, and Whitby on the|Yorkshire coast.||It w 

                                                                           30 chars                                                             

lington, No. 7, The|Crescent, Whitby, another to Herr Leutner, Var 

You do not have to use the line-by-line method for this problem. You can use any method you want to use. (Loading the text all-at-once makes this problem a bit easier) 

3.       Your output should go to a new text file named after the search details:   InputFileName_SearchString.txt Note the file extension “.txt” should be removed from the input file name as is shown in the names below 

    Example Output (Search String: “Whitby”), Output file name: Dracula_Whitby.txt  

1)       he other two were Exeter, and Whitby on the|Yorkshire coast.||It w 

2)       lington, No. 7, The|Crescent, Whitby, another to Herr Leutner, Var   3)  MURRAY'S JOURNAL|||_24 July. Whitby._--Lucy met me at the station       . . . 

41)  did.|Oh, why did I ever go to Whitby? There now, crying again! I w 

42)  ndows of St. Mary's Church at Whitby. I knew, too, the|red scar on 

43)  or fly on shore, as he did|at Whitby. But if the day come before h 
 

Example Output (Search String: “Borgo”), Output file name: Dracula_Borgo.txt

1)       ally on the frontier--for the|Borgo Pass leads from it into Bukov 

2)       on it is kept for you. At the Borgo|     Pass my carriage will aw   3) on losing no|time in reaching Borgo Prund. I was told that this r       . . . 

9)    hey|would be about now at the Borgo Pass. God guide and help them 

10)  by morning we shall reach the Borgo Pass. The|houses are very few 

11)  go|unrecorded.||We got to the Borgo Pass just after sunrise yeste
4.       In your python file, be sure to define the function first, then add the user-interaction, function invocation, and output generation afterwards.

More products