+1 (315) 557-6473 

Create a Program to Implement Container Objects in Python Assignment Solution.


Instructions

Objective
Write a python assignment to implement container objects.

Requirements and Specifications

program to implement container objects in python
program to implement container objects in python 1

Source Code

class Container:

def __init__(self):

"""

Create an empty container

"""

self.__items = list()

def insert(self, item):

"""

Append an element at the end of the container

"""

self.__items.append(item)

def erase_one(self, item):

"""

Delete the first occurrence of the item

"""

if item in self.__items:

self.__items.remove(item)

def erase_all(self, item):

"""

Delete all occurrence of the item in the list

"""

while item in self.__items:

self.__items.remove(item)

def count(self, item):

"""

Count the occurrence of item in the list

"""

occurrence = 0

for some_item in self.__items:

if some_item == item:

occurrence += 1

return occurrence

def items(self):

"""

Return distinct items in the list

"""

distinct_items = list()

for item in self.__items:

if item not in distinct_items:

distinct_items.append(item)

return distinct_items

def __str__(self):

"""

Return a string representation of the container

"""

return str(self.__items)[1:-1]

def __repr__(self):

"""

Returns a printable representation of the container

"""

return str(self.__items)[1:-1]

def __getitem__(self, idx):

"""

Access an item from the list given the index

"""

return self.__items[idx]

def __add__(self, other_container):

"""

Return a new container that contains all the items

that is here and the other container

"""

new_container = Container()

new_container.__items = self.__items + other_container.__items

return new_container

def __sub__(self, other_container):

"""

Return a new container where items here does not exist

on the other container

"""

paired_indices = list()

for i in range(len(other_container.__items)):

paired_indices.append(False)

new_container = Container()

for i in range(len(self.__items)):

pair_found = False

for j in range(len(other_container.__items)):

if self.__items[i] == other_container.__items[j] and not paired_indices[j]:

pair_found = True

paired_indices[j] = True

break

if not pair_found:

new_container.__items.append(self.__items[i])

return new_container

def __mul__(self, other_container):

"""

Find the intersection of two containers

"""

paired_indices = list()

for i in range(len(other_container.__items)):

paired_indices.append(False)

new_container = Container()

for i in range(len(self.__items)):

pair_found = False

for j in range(len(other_container.__items)):

if self.__items[i] == other_container[j] and not paired_indices[j]:

pair_found = True

paired_indices[j] = True

break

if pair_found:

new_container.__items.append(self.__items[i])

return new_container