Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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


imageimageimageimageimageimage

? 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Communication In Person, In Print, Online

Authors: Amy Newman, Scot Ober

8th edition

1111533164, 978-1111533168

More Books

Students also viewed these Programming questions

Question

Who do they notice those qualities in?

Answered: 1 week ago