Need help with Intro to Computer Science 2 Python problem 3

Need help with Intro to Computer Science 2 (Python) problem:

3. Write a class Collection that represents a collection of items. The class supports six methods: init 0 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 collection. contains 0 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

Solution

from collections import MutableSequence

class Collection(MutableSequence):
\"\"\"A container for manipulating lists of hosts\"\"\"
maxsize=10;
def __init__(self, data=None):
\"\"\"Initialize the class\"\"\"
super(Collection, self).__init__()
if (data is not None):
self._list_collection = list(data)
else:
self._list_collection = list()
Collection.maxsize=10

def __repr__(self):
return \"<{0} {1}>\".format(self.__class__.__name__, self._list_collection)

def __len__(self):
\"\"\"List length\"\"\"
return len(self._list_collection)
def size(self):
return len(self._list_collection)
def __contains__(self,item):
if item in self._list_collection:
return True
return False


def __getitem__(self, ii):
\"\"\"Get a list item\"\"\"
return self._list_collection[ii]

def __delitem__(self, ii):
\"\"\"Delete an item\"\"\"
del self._list_collection[ii]

def __setitem__(self, ii, val):
# optional: self._acl_check(val)
return self._list_collection[ii]

def __str__(self):
return str(self._list_collection)

def insert(self, ii, val):
# optional: self._acl_check(val)
self._list_collection.insert(ii, val)

def add(self, val):
self.insert(len(self._list_collection), val)

if __name__==\'__main__\':
c = Collection()
print c.maxsize
c.add(6)
c.add(5)
c.add(4)
print c.__contains__(6)
print c.size()

=====================================================

akshay@akshay-Inspiron-3537:~/Chegg$ python collectio.py
10
True
3

Need help with Intro to Computer Science 2 (Python) problem: 3. Write a class Collection that represents a collection of items. The class supports six methods:
Need help with Intro to Computer Science 2 (Python) problem: 3. Write a class Collection that represents a collection of items. The class supports six methods:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site