×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Implement Container Objects in Python Assignment Solution

July 15, 2024
Dr. Olivia Campbell
Dr. Olivia
🇺🇸 United States
Python
Dr. Olivia Campbell holds a Ph.D. in Computer Science from the University of Cambridge. With over 800 completed assignments, she specializes in developing complex Python applications, including fitness trackers and exercise planners. Dr. Campbell's expertise lies in algorithm design and data analysis, ensuring optimal performance and accuracy in every project she undertakes.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use the REPL (Read-Eval-Print Loop) to test your code interactively, and practice writing concise, readable expressions to harness Scala’s powerful syntax effectively.
News
In 2024, universities abroad, like Dublin City University, have introduced cutting-edge programming courses, including Computer Engineering programs focusing on AI and machine learning.

Instructions

Objective

Write a python assignment to implement container objects.

Requirements and Specifications

program-to-implement-container-objects-in-python

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

Related Samples

Explore our comprehensive Python programming homework samples at ProgrammingHomeworkHelp.com. Our curated examples cover a wide array of Python concepts and applications, providing clarity and guidance for your assignments. Whether you're delving into data analysis, web development, or machine learning, our samples are designed to assist you in mastering Python programming effectively.