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

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


? Q Lesson 05 - Programming Exercis X MindTap - Cengage LearningX Q The remove() method resizes the x + 8 https://ng.cengage.com/static/nb/ui/evo/index.html?deploymentid=5917702479999730828444230687&elSBN=9781337560191&id=17... AlProgramming Exercise 5.5 0 Instructions = .... & CENGAGE MINDTAP 20 78FMostly clear In the linkedbag.py file complete the following: 1. Define theclone () method to the LinkedBag class. o Returns a copy ofself. For example, the variable bag2 would contain the numbers 2, 3

? Q Lesson 05 - Programming Exercis X MindTap - Cengage Learning X Q The remove() method resizes the x + 8 https://ng.cengage.com/static/nb/ui/evo/index.html?deploymentid=5917702479999730828444230687&elSBN=9781337560191&id=17... Al Programming Exercise 5.5 0 Instructions = .... & CENGAGE MINDTAP 20 78F Mostly clear In the linkedbag.py file complete the following: 1. Define the clone () method to the LinkedBag class. o Returns a copy of self. For example, the variable bag2 would contain the numbers 2, 3 and 4 at the end of the following code segment: bag1 = ArrayBag([2,3,4]) bag2 bagl.clone () bag1== bag2 # Returns True bagl is bag2 # Returns False Grading Write your Python code in the code. FILETREE ~/sandbox arraybag.py arrays.py baginterface.py linkedbag.py node.py testbag.py Search 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 arraybag.py otherwise.""" x testbag.py return len(self) == 0 X daf # Accessor methods def isEmpty(self): """Returns True if len(self) == 0, or False linkedbag.py GA Q Search this course deflen__(self): """Returns the number of items in self.""" return self.size add (oolf other). x + yield self.items [cursor] cursor += 1 def_str__(self): """Returns the string representation of self." return "{" +", ".join(map(str, self)) + "}" defiter__(self): """Supports iteration over a view of self.""" cursor = 0 while cursor < len(self): 63 X

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!