PYTHON This is an single assignment and i need a complete code of it. Half work
ID: 3890994 • Letter: P
Question
PYTHONThis is an single assignment and i need a complete code of it.
Half work is done but where it says input your code here you have to add the code there
This the code and you have to finish it. Inputing the code in this assignment where it says add your code here! 1. Introduction In this exercise, we will complete three functions to simulate three different methods for file organization 2. Objectives The purpose of this assignment is to help you Refresh knowledge on list and function in Python. Refresh knowledge on reading files in Python. Do a little bit study on different file organizing methods. . Note: Before you start, if you are not familiar with list,for loop or function in Python, you are recommended to review the sample codes we covered in lecture first. 3. Background 3.1. Filing and Piling In our daily lives, we are always dealing with a lot of different files. As a college student, we need to organize our graded paper homework, exams, lecture notes etc. Some of us may just insert documents randomly into a big drawer and whenever we need to fetch a specific document in our storage, we may do a linear search starting from the leftmost document to the right. If we are careful enough, we cannot miss the one we want to find. After using the document, we may randomly insert the document back into the drawer. And whenever we need a certain document again, we can redo the linear search and random insert process which is simple but reliable Let's consider the time complexity of the random insert and linear search. Random insert takes constant time since what we need to do is just to open the drawer and put the file in randomly Nothing seems easier than that. But for linear search, given a pile of documents, in order to find a specific one, we have to check each of them linearly starting from leftmost one up to the one we want. So, how many documents do we have to pass before finding the one we want? If the documents are randomly inserted into the pile, a certain document has an equal possibility of locating at each position in the pile. So, if there are in total n documents, the expected number of documents we have to pass before finding the specific one is around n/2. In this assignment, we assume linear search is always conducted from the leftmost location of the pile to right.
Explanation / Answer
def leftmostfiling(L): M=[x for x in range(20)] count = 0 for i in L: for j in M: if i==j : M.remove(i) M=[i]+M break else: count += 1 return count def rightmostfiling(L): M=[x for x in range(20)] count = 0 for i in L: for j in reversed(M): if i==j : M.remove(j) M=M+[i] break else: count += 1 return count def fixedfiling(L): M=[x for x in range(20)] count = 0 for i in L: for j in M: if i==j: break else: count += 2 return count ### Do not modify anything below, for testing purposes only. # Random requests # print("Random request") # L = [int(line) for line in open("data.txt")] # print("Cost for leftmost filing :", leftmostfiling(L)) # print("Cost for fixed filing :",fixedfiling(L)) # print("Cost for leftmost filing :", leftmostfiling(L)) # print("Cost for fixed filing :",fixedfiling(L)) # print("Cost for rightmost filing :",rightmostfiling(L)) # Always request first item print(" Always request first item") L = [0]*100000 print("Cost for leftmost filing :", leftmostfiling(L)) print("Cost for fixed filing :", fixedfiling(L)) print("Cost for rightmost filing :", rightmostfiling(L)) # Always request last item print(" Always request last item") L = [19]*100000 print("Cost for leftmost filing :", leftmostfiling(L)) print("Cost for fixed filing :", fixedfiling(L)) print("Cost for rightmost filing :", rightmostfiling(L)) # Repeated requests print(" Repeated requests #1") L = [x for x in range(20)]*5000 print("Cost for leftmost filing :", leftmostfiling(L)) print("Cost for fixed filing :", fixedfiling(L)) print("Cost for rightmost filing :", rightmostfiling(L)) print(" Repeated requests #2") L = [x for x in range(10)]*10000 print("Cost for leftmost filing :", leftmostfiling(L)) print("Cost for fixed filing :", fixedfiling(L)) print("Cost for rightmost filing :", rightmostfiling(L)) print(" Repeated requests #3") L = [x + 10 for x in range(10)]*10000 print("Cost for leftmost filing :", leftmostfiling(L)) print("Cost for fixed filing :", fixedfiling(L)) print("Cost for rightmost filing :", rightmostfiling(L))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.