Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 . Write a class Collection that represents a collection of items. The class supports six methods: a . _ _ init _ _ (

3. Write a class Collection that represents a collection of items. The class supports six methods:
a.__init__() 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 10 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.
b.__contains__() 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.
e.__str__() which returns a string representing the object as seen in the sample output below.
f.__repr__() which returns a string representing the object as seen in the sample output below.cl= Collection()
ct
[]
) cl.add('train')
True
cl.add('block')
True
cl
['train', 'block']
'block' in cl
True
) 'BLOCR' in cl
false
cl.add('doll')
True
print (c1)
['train', 'block', 'dol11']
cl.add('dol1')
Talse
cl
['train', 'block', 'dol1']
cl.size (])
3
c2= Collection(2)
c2.add(1)
True
c2.add(2)
True
c2
1,2
c2.add(3)
Talse
print (c2)
1,2
image text in transcribed

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

Step: 3

blur-text-image

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

Building The Data Lakehouse

Authors: Bill Inmon ,Mary Levins ,Ranjeet Srivastava

1st Edition

1634629663, 978-1634629669

More Books

Students also viewed these Databases questions

Question

8. How would you define your organizations managerial philosophy?

Answered: 1 week ago