Question: Python 3 Coding: Below are the functions I need help with. Thank you! def locate(filename,s): Given a string filename and a string s to search
Python 3 Coding: Below are the functions I need help with. Thank you!
def locate(filename,s): Given a string filename and a string s to search for in the file, return an ordered list of all line numbers corresponding to the lines containing at least one occurrence of the string s (case sensitive). o Assumptions: (1) file indicated by filename exists in the current directory; (2) file can be empty; (3) line number starts with 1; and (4) s is a non-empty string that does not span more than a single line. o Hint: in can be used to check for substrings Examples given the file to the right: o locate ("in.txt","up") [1,3] o locate ("in.txt","ill") [1,2,3] o locate ("in.txt","will") []
def store(d,filename): Given a dictionary d and a string filename, create a new file named filename and output the content of d into that file. You can assume that the dictionary d always has the key:value format as string:[list,of,integers]. Every key-value pair of the dictionary should be output as: a string that starts with key, followed by ":", a tab, then the integers from the value list. Every integer should be followed by a "," and a tab except for the very last one, which should be followed by a newline. Multiple items of the dictionary must be sorted asciibetically by their keys. See the example below. o Assumptions: (1) if a file named filename already exists, then the content should be overwritten; (2) dictionary d could be empty; (3) the value list could be empty; and (4) the function returns None. Examples: o d = {'orange':[1,3],'apple':[2]} o store(d,"out.txt") should end up with a file to the right # the file contents should be read as this string: # "apple:\t2 orange:\t1,\t3 "
def append_total(filename): Given a string filename of a file containing one integers (one per line), calculate the total of all the integers and append the line "Total:" followed immediately by the integer total (no spaces in between) and ending with a newline. See example. o Assumptions: (1) file indicated by filename exists in the current directory; (2) file can be empty, and if so the sum will be 0; (3) a non-empty file can contain one or more lines ending with a newline; (4) each line contains a single integer and nothing else, and (5) the function returns None. Examples: o append_total("f1.txt") corresponds to the files to the right
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
