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

Submit Ruh Grades Reset 1 #We\'ve given you a file called \"top500.txt\" which c

ID: 3703060 • Letter: S

Question

Submit Ruh Grades Reset 1 #We've given you a file called "top500.txt" which contains Console output will be displayed here #the name and lifetime gross revenue of the top 500 3 #films at the time this.question was written 4# 5 #Each line of the given file is f ormatted like so: 6 # ?name> cgross revenue in dollars» 8 #In other words, you should call ·split(" ") to split 9 #the line into the movie name (the first item) and the 10 #gross (the second item) 11 # 12 #Unfortunately, these moviesare not in order. Write a 13 #function called "sort films" that accepts two parameters : 14 #a string of a source filename and a string of a 15 #destination filename. Your function should open the 16 #source file and sort the contents from greatest gross 17 #revenue to least gross revenue, and write the sorted 18 #contents to the destination filename. You may assume the 19,#source file is correctly formatted. 20 21 22 # rite your function here! 23 24 25 #The line of code below will test your function and put 26 #your results in topseeresult.txt. Then, it will print 27 #"Done!" 28 sort fi0.txt", "topsooresult.txt") 29 print "Done!") 30 31 32 34

Explanation / Answer

def sort_files(file1,file2): films={} with open (file1,'r') as f: for line in f: movie,revenue=line.split(' ') films[int(revenue)]=movie f=open(file2,'w') for i in sorted(films,reverse=True): f.write('{:s} {:d} '.format(films[i],i)) f.close()