def locate(filename,s): Given a string filename and a string s to search for in
ID: 3817414 • Letter: D
Question
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: 2 orange: 1, 3 "
• 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
Explanation / Answer
# pastebin code link: https://pastebin.com/y50zKkin
def locate(filename,s):
i = 0
lineList = []
with open(filename, "r") as fh:
for line in fh:
i += 1
if s.lower() in line.lower():
lineList.append(i)
return lineList
def store(d,filename):
with open(filename, "w") as fw:
for key in sorted(d):
fw.write(key + ": ")
if d[key][0]:
fw.write(str(d[key][0]))
for i in range(1, len(d[key])):
fw.write(", " + str(d[key][i]))
fw.write(" ")
def append_total(filename):
total = 0
with open(filename, "r") as fh:
for line in fh:
i = int(line)
total += i
with open(filename, "a") as fw:
fw.write("Total:"+str(total)+" ")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.