Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3 . Write a class Collection that represents a collection of items. The class supports six methods: a . _ _ init _ _ (
Write a class Collection that represents a collection of items. The class supports six methods:
ainit which takes an optional parameter representing the maximum size of the collection. It sets the maximum size of the collection to the provided value or to the value if no number is given as a parameter. It also creates an empty list to store the items in the
if no number is given as a parameter. It also creates an empty list to store the items in the collection.
bcontains which takes an item as a parameter and returns True if the item is already in the collection and False otherwise. The function must return a Boolean and not a string.
c add which adds a new item to the collection. It does this by first checking that the maximum capacity for the collection hasn't been reached. If the maximum capacity has been reached, then the method returns False without modifying the collection. If the maximum capacity has not yet been reached, the method checks that the item is not already in the collection using the contains method described above. If the item is already there, the method returns False and does not modify the collection. If the item is not present and the maximum capacity has not yet been reached, the method adds the item to the collection and returns True. The function must return a Boolean and not a string.
d size which returns the number of items currently in the collection.
estr which returns a string representing the object as seen in the sample output below.
frepr which returns a string representing the object as seen in the sample output below. Collection
claddtrain
True
claddblock
True
train 'block'
'block' in
True
'BLOCR' in
false
cladddoll
True
print c
train 'block', 'dol
cladddol
Talse
train 'block', 'dol
clsize
Collection
cadd
True
cadd
True
cadd
Talse
print c
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started