Assignment. This and the next question assess your unde
Added on - Sep 2019
Showing pages 1 to 2 of 4 pages
AssignmentThis and the next question assess your understanding to solve computational problems bydesigning, implementing and analysing algorithms and by using ADTs.Question 1Code files to download:TMA01_Q3_Bookcase.pyandTMA01_Q3.pyCode file that you canonly modify:TMA01_Q3.pyConsider a media management application that manages personal media on computing devices,including books, films, music and video games. It allows adding, removing, searching items, etc.You can think of a virtual bookcase containing shelves on which to keep items, as in theapplication shown in Figure 1, with rows and columns.Figure1User interface of Apple iBooksFollowing good practice, we implement a Bookcase ADT in Python as a class, given infileTMA01_Q3_Bookcase.py.This file must not be modified.This is an example of usingencapsulation to hide the internal representation of the bookcase from the user or client. Youshould have read about the advantages of this approach in Unit 2.In parts (a) to (c) you are asked to implement three functions that use the Bookcase ADT. Pleasemake sure you use the ADT, otherwise you will be penalised. Once you added the code for allfunctions to fileTMA01_Q3.py, you can check it by running the tests in that file.a. Consider the computational problem of finding an empty space (any one will do) in a bookcase.Problem: findSpaceInputs: a BookcaseaBookcasePreconditions:aBookcasehas at least one free spaceOutputs: a sequence of two integers(row, column)Postconditions:aBookcasehas no item at the returnedrowandcolumni.) Write the initial insight for an algorithm to solve the problem. Your algorithmmust use one or more operations of the Bookcase ADT.ii.) Implement the algorithm you described, using a tuple to represent theoutput sequence. The function headerdef findSpace(aBookcase):is given in fileTMA01_Q3.py. Include anassertstatement for the precondition.
b. Consider the computational problem of adding an item to the bookcase.Problem: addItemInputs: a BookcaseaBookcase; two stringsnameandmediaTypePreconditions: neither string is emptyOutputs: a booleanaddedPostconditions:addedis false ifaBookcasehad no free position, otherwise it is trueandaBookcasehas one free position less, which now has an item with thegivennameandmediaTypei.) Write the initial insight for implementing an algorithm to solve the problem.Use thefindSpacefunction of part (a).ii.) Implement the algorithm you have just described. The function headerdef addItem(aBookcase, name, mediaType):is in the same file. Include any necessaryassertstatements.c. Consider the computational problem of counting how many items are there of a given mediatype (e.g. film, book, or music).Problem: countItemsInputs: a BookcaseaBookcase; a stringmediaTypePreconditions:mediaTypeisn’t emptyOutputs: an integercounterPostconditions:counteris the number of items inaBookcasethat are of thegivenmediaTypeWrite the code to solve that problem. The function headerdef countItems(aBookcase, mediaType):and assertion code are already provided.d. Estimate theworst-caseexecution time T(r, c) and the run-time complexity of the BookcasemethodgetEmptySpaces, whereris the number of rows/shelves of the bookcase andcis thenumber of places per row/shelf. T(r, c) should express thenumber of assignmentsinvolved incounting the empty spaces on the bookcase. Explain your answer.(25marks)