Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please include the main and test functions Problem 2. Non-interactive Text Editing A non-interactive text editor executes simple editing commands on a text file using
Please include the main and test functions
Problem 2. Non-interactive Text Editing A non-interactive text editor executes simple editing commands on a text file using a programming interface (i.e. functions or object methods) in some programming language, like Python. All commands take as input, at minimum, the file name as a parameter, then perform their job (reading or modifying the file), and end returning some results. For this problem we assume the text files have the utf-8 encoding (the default in Python) and they are not too large. Therefore, it's OK to read the file entirely to memory, execute the operations required, then overwrite the file with the modified file content, as a string. This is not the only approach, but it's the simpler one and it works for smaller files. Here are the editing commands, with the corresponding function signatures: ed_read(filename, from-0, to--1): returns as a string the content of the file named filename, with file positions in the half-open range [from, to). If to-1, the content between from and the end of the file will be returned. If parameter to exceeds the file length, then the function raises exception ValueError with a corresponding error message ed-find(filename, search-str): finds string search-str in the file named by filename and returns a list with index positions in the file text where the string search_str is located. E.g. t returns [4, 100] if the string was found at positions 4 and 100. It returns [] if the string was not found. ed_replace(filename, search_str, replace_with, occurrence--1): replaces search_str in the file named by filename with string replace_with. If occurrence-1, then it replaces ALL occurrences. If occurrence>-0, then it replaces only the occurrence with index occurrence, where 0 means the first, 1 means the second, etc. If the occurrence argument exceeds the actual occurrence index in the file of that string, the function does not do the replacement. The function returns the number of times the string was replaced. ed append(filename, string): appends string to the end of the file. If the file does not exist, a new file is created with the given file name. The function returns the number of characters written to the file ed_write(filename, pos_str_col): for each tuple (position, s) in collection pos_str_col (e.g. a list) this function writes to the file at position pos the string s. This function overwrites some of the existing file content. If any position parameter is invalid ( 0) or greater than the file contents size the function does not change the file and raises ValueError with a proper error message. In case of no errors, the function returns the number of strings written to the file. Assume the strings to be written do not overlap ed insert(filename, pos_str_col): for each tuple (position, s) in collection pos_str_col (e.g. a list) this function inserts into to the file content the string s at position pos. This function does not overwrite the existing file content, as seen in the examples below. If any position parameters is invalid (-0, then it replaces only the occurrence with index occurrence, where 0 means the first, 1 means the second, etc. If the occurrence argument exceeds the actual occurrence index in the file of that string, the function does not do the replacement. The function returns the number of times the string was replaced. ed append(filename, string): appends string to the end of the file. If the file does not exist, a new file is created with the given file name. The function returns the number of characters written to the file ed_write(filename, pos_str_col): for each tuple (position, s) in collection pos_str_col (e.g. a list) this function writes to the file at position pos the string s. This function overwrites some of the existing file content. If any position parameter is invalid ( 0) or greater than the file contents size the function does not change the file and raises ValueError with a proper error message. In case of no errors, the function returns the number of strings written to the file. Assume the strings to be written do not overlap ed insert(filename, pos_str_col): for each tuple (position, s) in collection pos_str_col (e.g. a list) this function inserts into to the file content the string s at position pos. This function does not overwrite the existing file content, as seen in the examples below. If any position parameters is invalid (Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started