Ingeneral,acompactlistofintegersrepresentsasetofintegersasalist?x0,x1,x2,...?ofintegerssuchthatx0< x1<x2<...,withthemeaningthatallintegersfromx0tox1?1,aswellasallfromx2tox3?1,andsoon,areincludedintheset.Thus,theincludedintegersstartatx0,thenthefirstintegerlargerthanx0nottobeincludedisx1,thenthenextintegerafterx1tobe includedisx2and soon.The empty setisrepresentedasan empty list(of length0).Ifthecompactlisthasanoddlength,itcontainsallvaluesfromthelastentryinthelistuptothemaximum valueMAX.Asafinalexample,thesetofallintegersisrepresentedbytheone-elementcompactlist?MIN?:thatis,itconsistsof thesinglenumberMIN.You havetoimplementa classcalledCompactListwhich implementsvarioussetoperations,asdescribedbelow.Beforewedescribethevariousfunctionalities,wequicklyreviewthebasicsetoperationsandrelations,toestablishnotation.GivensetsAandB,theirunionisthesetA∪Bdefinedasthesetof allelementsthatareinAorinBorinbothAandB,theirintersectionisthesetA∩BdefinedasthesetofallelementsthatareinAandinB,theirdiferenceisthesetA\Bdefinedasthesetof allelementsthatareinAbutnotinB.The setAisasubsetof setB,writtenA?B,ifeach elementofAisalsoan elementofB.ThesetsAandBareconsideredtobeequaliftheyhavethesame elements.Ifallthesetsthatwe considerarecontainedinsomeuniversalsetU,thenthecomplementofsetAisthesetAc=U\A.Below,wheneverwementionunion,intersection,diferenceorcomplement,we alwaysrefertothesetsrepresentedbycompactlistsof integers.Forexample,theunionofthesetA={10,...,19}representedbythecompactlist?10,20?,andthesetB={30,...,39}representedbythecompactlist?30,40?,isthesetA∪Brepresentedbythecompactlist?10,20,30,40?.Asanotherexample,theintersectionofthesetAwiththesetC={15,...,24},representedby thecompactlist?15,25?,isthesetA∩C={15,16,17,18,19}representedbythecompactlist?15,20?.Forthepurposesofoursetoperations,theuniversalsetistheset{MIN,MIN+1,...,MAX}ofintegers,andsowe takecomplements withrespecttothisset.Forexample,thecomplementofsetAistheset{MIN,...,9}∪ {20,...},representedby thecompactlist?MIN,10,20?.Thefollowinglist(a)–(m)describesthefunctionalitiesthatyouhavetoimplement.YoucanusethetemplatefileCompactList.pyon Moodle.(a)ConstructorforacompactlistfromaPython list:__init__(alist)Createsacompactlisttorepresentthesetof integersgivenbythePython listalist.Asan example,CompactList([8,3,5,4])initialisesthecompactlist?3,6,8,9?.(b)Instancemethod:cardinality()Returnsthenumber of elementsinthesetrepresentedbythiscompactlist.The worst-casetimeshouldbeO(n)wherenisthelengthof thecompactlist.(c)Instancemethod:insert(value)Insertsvalueintothe setrepresentedby thiscompact listifvalueisnotalready containedintheset.The worst-casetimeshouldbeO(n)wherenisthelengthof thecompactlist.(d)Instancemethod:delete(value)Removesvaluefrom the setrepresented by thiscompact listifvalueisinthe set,otherwisedoesnothing.The worst-casetimeshouldbeO(n)wherenisthelengthof thecompactlist.(e)Instancemethod:contains(value)ReturnsTrueifvalueisanelementof thesetrepresentedbythiscompactlist,otherwisereturnsFalse.The worst-casetimeshouldbeO(logn),wherenisthelengthof thiscompactlist.
WeChat:yj4399_