Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python Docs needed: https://drive.google.com/open?id=1QQhlp-kQDNIESen61fVanpLoAp

ID: 3713563 • Letter: P

Question

python

Docs needed:

https://drive.google.com/open?id=1QQhlp-kQDNIESen61fVanpLoApl51YaK

1. 25 Points: Write a class Bag that represents a collection of items. You must write the entire class yourself. A template is not included in the py file, you must create the Bag class and Iterator yourself. The class supports six methods Bag Class: a init O which takes an optional parameter representing the maximum amount of items that can be stored in the locker. It sets the maximum size of the collection to the vided 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 but does not allow passing a list via the constructor as a parameter b_contains _() which takes an item as a parameter and returns True if the item is alreadv in the collection and False otherwise. The function must return a Boolean and not a string. Note: The in operator uses contains c. addO which adds a new item to the Locker. 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. size0 which returns the number of items currently in the collection. Q which returns a string representing the obiect as seen in the sample output e str below g iter () which returns an iterator for the class. The iterator should move from the mostly recently added item to the item first added to the Bag. See the sample output below for examples. Please note that whatever classes you need for this questiorn should be included in the file you submit. You should not reference any files other than the csc242hw4.py file

Explanation / Answer

Bag Class:

Python Code:

class Bag:

    Bsize = None

    data = None

    itr = None

    def __init__(self, maxSize=10):

        self.Bsize = maxSize

        self.data = []

    def __contains__(self, item):

        return item in self.data

    def add(self,item):

        if len(self.data) >= self.Bsize:

            return False

        if self.__contains__(item):

            return False

        self.data.append(item)

        return True

    def size(self):

        return self.Bsize

    def __str__(self):

        string = "Collection is of size: " + str(self.Bsize) + " "

        string = string + "Collection is: " + str(self.data)

        return string

    def __iter__(self):

        self.itr = -1

        return self

    def next(self):

        if abs(self.itr) <= self.Bsize:

            result = self.data[self.itr]

            self.itr -= 1

            return result

        else:

            raise StopIteration()