Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you please help me code this for python?! Def store (d, filename): Given a d

ID: 3816434 • Letter: C

Question

Can you please help me code this for python?!

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. Assumptions: (1) if a file named filename already exists, then the content out.txt should be overwritten; (2) dictionary d could be empty (3) the value list could be empty; and (4) the function returns None. Examples d = {' orange': [1, 3], ' apple':[2]} 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 n'' 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 before after the line "Total:" followed immediately by the integer total (no spaces in between) and ending with a newline. See example. 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 3 non-empty file can contain one or more lines ending with a newline; (4) each line contains a single integer and nothing else, 9 and (5) the function returns None. Examples: append_total("f1.txt") corresponds to the files to the right

Explanation / Answer

def store(d,filename):
   f = open(filename,'w')
   keys = d.keys()
   keys.sort()
   for k in keys:
       f.write(k+": ")
       lt = d[k]
       ln = len(lt)
       if(ln==0):
           f.write(" ")
           continue
       f.write(str(lt[0]))
       for i in range(1,ln):
           f.write(", "+str(lt[i]))
       f.write(" ")
d = {'orange':[1,3],'apple':[2]}
store(d,"newdict.txt")