Question: Problem # 1 Write a python function char _ ind which accepts two parameters, a string and a character ( char ) , which in

Problem #1
Write a python function char_ind which accepts two parameters, a string and a character (char), which in Python will also have the datatype string. The function should return a list with the indices at which the character appears in the string. If the character does not appear in the string, the function should return an empty list.
Function Name: char_ind
Parameters: string, string
Return Value: list
Example: if I pass the parameters 'Hello World!' and 'l', the return value should be [2,3,9]. However, if I pass the parameters 'Welcome' and 'a', the return value should be []
Problem #2
Write a python function read_char_ind which accepts two parameters, a filename (string) and a character (also a string). The function should return a list with the indices at which the character appears in the text contained in the file. This index should be based on character count only. If the character does not appear in the file, the function should return an empty list. You MUST call the function from Problem 1 to process the string. If an IOError is thrown when opening the file, the function should return the None value and print an appropriate message. You MUST use a try-except control statement, and close the file in a finally clause.
Function Name: read_char_ind
Parameters: string, string
Return Value: list (or None)
Example: if I pass the filename 'file1.txt', and file1.txt contains
Hello World!
This is a file
that needs processing and I pass 'l' for the character, the return value should be [2,3,9,25]
If I pass the filename for the same file as the previous example, and pass 'z' for the character, the return value should be []
Problem #3
Write a python function read_char_lines which accepts two parameters, a filename (string)and a character (also a string). The function should return a two-dimensional list which contains the number of times the character appears in each line of the file. You MUST call the char_ind function from Problem 1 to process the string for each line. If an IOError is thrown when opening the file, the function should return an empty list [](which should NOT be two-dimensional). If the file exists but is empty, you should also return an empty list. You MUST use a with statement to handle the file
Function Name: read_char_lines
Parameters: string, string
Return Value: list
Example: if I pass the filename 'file1.txt', and file1.txt contains
Hello World!
This is a file that needs processing
and I pass 'l' for the character, the return value should be [[2,3,9],[12],[]]
If I pass the filename for the same file as the previous example, and pass 'z' for the character, the return value should be []
Problem #4
Write a python function copy_file which copies a file. The function accepts two parameters, the second of which should be optional (i.e. it has a default value). The first parameter is a filename (string) the second parameter is a text string which will be inserted at the start of the copied (new) file. The default value should be '#This is a copy'. The function will return True if the file is successfully created, False if any IO errors are thrown when creating and copying the file. Using file reading and writing, the function should read the file specified by the filename, then create a copy. The name of the new file should be copy_ where is the name of the original file (Hint use string concatenation). Insert the second parameter at the top of the contents of the new file with a newline character separating the parameter from the original text
Function Name: copy_file
Parameters: string, (optional) string
Return Value: boolean
Example: if I pass the filename 'file1.txt' and pass only the filename, and file1.txt contains
Hello World!
This is a file
that needs processing
the new file will have the filename 'copy_file1.txt' and contains
# This is a copy
Hello World!
This is a file
that needs processing
Problem #5
Define a Python function named main. Within the main function, prompt the user to provide, as input, the arguments for each of the functions defined in Problems 1-4. The main function will call each of the functions with the specified inputs, and display the return value for each function call (see examples below).
Function Name: Main
Parameters: N/A
Return Value: N/A
Make sure you call the main() function itself at the end of your script. The code should be similar to the code below.
Note: You can assume that all files will be in the same directory as the main python file
Note2: Although in both examples we provide the same file for all problems, we should be able to specify different files (if
def main():
...
if __name__=='__main__':
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!