Please help with the following code, text files are provided, thank you so much for your time.
Question:
Please help with the following code, text files are provided, thank you so much for your time. im new to programming.
Arraybag.Py text file
"""
Project5.5
File:arraybag.py
Author:KenLambert
ReuseyoursolutionfromProgrammingExercise5.4asyourstarterfile
"""
fromarraysimportArray
classArrayBag(object):
"""Anarray-basedbagimplementation."""
#ReuseyoursolutionfromProgrammingExercise5.4asyourstarterfile
#Classvariable
DEFAULT_CAPACITY=10
#Constructor
def__init__(self,sourceCollection=None):
"""Setstheinitialstateofself,whichincludesthe
contentsofsourceCollection,ifit'spresent."""
self.items=Array(ArrayBag.DEFAULT_CAPACITY)
self.size=0
ifsourceCollection:
foriteminsourceCollection:
self.add(item)
#Accessormethods
defisEmpty(self):
"""ReturnsTrueiflen(self)==0,orFalseotherwise."""
returnlen(self)==0
def__len__(self):
"""Returnsthenumberofitemsinself."""
returnself.size
def__str__(self):
"""Returnsthestringrepresentationofself."""
return"{"+",".join(map(str,self))+"}"
def__iter__(self):
"""Supportsiterationoveraviewofself."""
cursor=0
whilecursor
yieldself.items[cursor]
cursor+=1
def__add__(self,other):
"""Returnsanewbagcontainingthecontents
ofselfandother."""
result=ArrayBag(self)
foriteminother:
result.add(item)
returnresult
def__eq__(self,other):
"""ReturnsTrueifselfequalsother,
orFalseotherwise."""
ifselfisother:returnTrue
iftype(self)!=type(other)or\
len(self)!=len(other):
returnFalse
foriteminself:
ifself.count(item)!=other.count(item):
returnFalse
returnTrue
defcount(self,item):
"""Returnsthenumberofinstancesofiteminself."""
total=0
fornextIteminself:
ifnextItem==item:
total+=1
returntotal
#Mutatormethods
defclear(self):
"""Makesselfbecomeempty."""
self.size=0
self.items=Array(ArrayBag.DEFAULT_CAPACITY)
Linkedbag.py Textfile
"""
Project5.5
File:linkedbag.py
Author:KenLambert
"""
fromnodeimportNode
classLinkedBag(object):
"""Alink-basedbagimplementation."""
#Constructor
def__init__(self,sourceCollection=None):
"""Setstheinitialstateofself,whichincludesthe
contentsofsourceCollection,ifit'spresent."""
self.items=None
self.size=0
ifsourceCollection:
foriteminsourceCollection:
self.add(item)
#Accessormethods
defisEmpty(self):
"""ReturnsTrueiflen(self)==0,orFalseotherwise."""
returnlen(self)==0
def__len__(self):
"""-Returnsthenumberofitemsinself."""
returnself.size
def__str__(self):
"""Returnsthestringrepresentationofself."""
return"{"+",".join(map(str,self))+"}"
def__iter__(self):
"""Supportsiterationoveraviewofself."""
cursor=self.items
whilenotcursorisNone:
yieldcursor.data
cursor=cursor.next
def__add__(self,other):
"""Returnsanewbagcontainingthecontents
ofselfandother."""
result=LinkedBag(self)
foriteminother:
result.add(item)
returnresult
defclone(self):
"""Returnsacopyofself."""
def__eq__(self,other):
"""ReturnsTrueifselfequalsother,
orFalseotherwise."""
ifselfisother:returnTrue
iftype(self)!=type(other)or\
len(self)!=len(other):
returnFalse
foriteminself:
ifnotiteminother:
returnFalse
returnTrue
#Mutatormethods
defclear(self):
"""Makesselfbecomeempty."""
self.size=0
self.items=None
defadd(self,item):
"""Addsitemtoself."""
self.items=Node(item,self.items)
self.size+=1
defremove(self,item):
"""Precondition:itemisinself.
Raises:KeyErrorifiteminnotinself.
Postcondition:itemisremovedfromself."""
#Checkpreconditionandraiseifnecessary
ifnotiteminself:
raiseKeyError(str(item)+"notinbag")
#Searchforthenodecontainingthetargetitem
#probewillpointtothetargetnode,andtrailer
#willpointtotheonebeforeit,ifitexists
probe=self.items
trailer=None
fortargetIteminself:
iftargetItem==item:
break
trailer=probe
probe=probe.next
#Unhookthenodetobedeleted,eitherthefirstoneorone
#thereafter
ifprobe==self.items:
self.items=self.items.next
else:
trailer.next=probe.next
#Decrementlogicalsize
self.size-=1
Business Communication In Person, In Print, Online
ISBN: 978-1111533168
8th edition
Authors: Amy Newman, Scot Ober