Adding methods to the ListBag class
In the file ListBag.py, add the methods described below to the ListBag class, and then add code
to test these methods. You should not add any new instance variables (fields) to the class.
def isEmpty(self)
This method should return True if the ListBag is empty, and False otherwise.
def numOccur(self, item)
This method should return the number of times that the parameter item occurs in the called
ListBag. For example, if b1 represents the bag
{7, 5, 3, 5, 7, 2, 7}, then b1.numOccur(2) should return 1, b1.numOccur(7) should return 3, and
b1.numOccur(20) should return 0.
def addItems(self, other)
This method should attempt to add to the called ListBag all of the items found in the parameter
other. As a result, your method should use method calls to access the internals of that bag. See
our implementation of the containsAll() method for an example of this.
def equals(self, other)
This method should determine if the called ListBag is equal to the parameter other. Two bags are
equal if they contain the same items. The location of the items in the bags does not matter (e.g.,
{5, 6, 6, 7} is equal to {7, 6, 5, 6}), but bags that are equal must have the same number of
occurrences of each item (e.g., {5, 6, 6, 7} is not equal to {5, 6, 7}). The method should return
True if the two bags are equal, and False otherwise (including cases in which the parameter is
None). Your method should use method calls to access the internals of that bag. See our
implementation of the containsAll() method for an example of this. You are strongly encouraged
you to have yourequalsmethod take advantage of one of the other methods that you are
implementing for this problem.
def unionWith(self, other)
This method should create and return an ListBag containing one occurrence of any item that is
found in either the called object or the parameter other. For full credit, the resulting bag should
not include any duplicates. For example, if b1 represents the bag {2, 2, 3, 5, 7, 7, 7, 8} and b2
represents the bag {2, 3, 4, 5, 5, 6, 7}, then b1.unionWith(b2) should return an ListBag
representing the bag {2, 3, 4, 5, 6, 7, 8}. If there are no items in either bag, the method should
return an empty ListBag. As a result, your method should use method calls to access the internals
of that bag. See our implementation of the containsAll() method for an example of this.
---------------------------------------------------------------------------------------------------------------
# An implementation of a Bag Class using a list.
from random import randrange
class ListBag:
# Default, no-arg constructor - creates a new, empty ArrayBag with
# the default maximum size.
def __init__ (self):
self.items = None
self.numItems = 0
# add - adds the specified item to the Bag.
def add(self, item):
if item is None:
print(\"Added item is illegal\")
else:
if self.items is None:
self.items = []
self.items.append(item)
self.numItems += 1
else:
self.items.append(item)
self.nu.
Major project report on Tata Motors and its marketing strategies
Adding methods to the ListBag classIn the file ListBag.py, add the.pdf
1. Adding methods to the ListBag class
In the file ListBag.py, add the methods described below to the ListBag class, and then add code
to test these methods. You should not add any new instance variables (fields) to the class.
def isEmpty(self)
This method should return True if the ListBag is empty, and False otherwise.
def numOccur(self, item)
This method should return the number of times that the parameter item occurs in the called
ListBag. For example, if b1 represents the bag
{7, 5, 3, 5, 7, 2, 7}, then b1.numOccur(2) should return 1, b1.numOccur(7) should return 3, and
b1.numOccur(20) should return 0.
def addItems(self, other)
This method should attempt to add to the called ListBag all of the items found in the parameter
other. As a result, your method should use method calls to access the internals of that bag. See
our implementation of the containsAll() method for an example of this.
def equals(self, other)
This method should determine if the called ListBag is equal to the parameter other. Two bags are
equal if they contain the same items. The location of the items in the bags does not matter (e.g.,
{5, 6, 6, 7} is equal to {7, 6, 5, 6}), but bags that are equal must have the same number of
occurrences of each item (e.g., {5, 6, 6, 7} is not equal to {5, 6, 7}). The method should return
True if the two bags are equal, and False otherwise (including cases in which the parameter is
None). Your method should use method calls to access the internals of that bag. See our
implementation of the containsAll() method for an example of this. You are strongly encouraged
you to have yourequalsmethod take advantage of one of the other methods that you are
implementing for this problem.
def unionWith(self, other)
This method should create and return an ListBag containing one occurrence of any item that is
found in either the called object or the parameter other. For full credit, the resulting bag should
not include any duplicates. For example, if b1 represents the bag {2, 2, 3, 5, 7, 7, 7, 8} and b2
represents the bag {2, 3, 4, 5, 5, 6, 7}, then b1.unionWith(b2) should return an ListBag
representing the bag {2, 3, 4, 5, 6, 7, 8}. If there are no items in either bag, the method should
return an empty ListBag. As a result, your method should use method calls to access the internals
of that bag. See our implementation of the containsAll() method for an example of this.
---------------------------------------------------------------------------------------------------------------
# An implementation of a Bag Class using a list.
from random import randrange
2. class ListBag:
# Default, no-arg constructor - creates a new, empty ArrayBag with
# the default maximum size.
def __init__ (self):
self.items = None
self.numItems = 0
# add - adds the specified item to the Bag.
def add(self, item):
if item is None:
print("Added item is illegal")
else:
if self.items is None:
self.items = []
self.items.append(item)
self.numItems += 1
else:
self.items.append(item)
self.numItems = self.numItems + 1
# remove - removes one occurrence of the specified item (if any)
# from the Bag. Returns true on success and false if the
# specified item (i.e., an object equal to item) is not in the Bag.
def remove(self, item):
for i in range(self.numItems):
if self.items[i] == item:
self.items.remove(item)
self.numItems = self.numItems - 1
return True
return False # item not found
# contains - returns true if the specified item is in the ListBag, and
# false otherwise.
def contains(self, item):
return item in self.items
3. # containsAll - does this ListBag contain all of the items in
# otherBag? Returns false if otherBag is null or empty.
def containsAll(self, otherBag):
if otherBag is None or otherBag.numItems == 0:
return False
other = otherBag.toList()
for i in range(len(otherBag.items)):
if not self.contains(otherBag.items[i]):
return False
return True
# numOfItems - returns the number of items in the Bag.
def numOfItems(self):
return self.numItems
# grab - returns a reference to a randomly chosen in the Bag.
def grab(self):
if self.numItems == 0:
print("the bag is empty")
return None
whichOne = randrange(0, self.numItems)
return self.items[whichOne]
# toList - return an list containing the current contents of the bag
def toList(self):
copy = ListBag()
for i in range(self.numItems):
copy.add(self.items[i])
return copy
# __str__ - converts this ListBag into a readable string.
def __str__(self):
s = "{"
for i in range(self.numItems - 1):
s = s + " " + str(self.items[i]) + ","
s = s + " " + str(self.items[-1]) + " }"
return s
4. # isEmpty - add method description
def isEmpty(self):
pass
# remove the pass statement and add you solution
# numOccur - add method description
def numOccur(self, item):
pass
# remove the pass statement and add you solution
# addItems - add method description
def addItems(self, other):
pass
# remove the pass statement and add you solution
# equals - add method description
def equals(self, item):
pass
# remove the pass statement and add you solution
# unionWith - add method description
def unionWith(self, item):
pass
# remove the pass statement and add you solution
# Test the ListBag implementation.
# Create an ListBag named bag1.
bag1 = ListBag()
bag1.add(None)
# Determine size of bag1
size = eval(input("Size of bag 1: "))
for i in range(size):
bag1.add(i)
# Print bag1
print("bag 1 = " + str(bag1))
print()
# Print number of items in bag1
print("bag 1 has " + str(bag1.numOfItems()) + " items.")
5. # Select a random item and print it.
item = bag1.grab()
print("grabbed " + str(item))
print("bag 1 = " + str(bag1))
# Iterate over the objects in bag1, printing them one per line.
for i in bag1.items:
print(i)
print()
# Get an item to remove from bag1, remove it, and reprint the bag.
itemStr = int(input("item to remove: "))
if bag1.contains(itemStr):
bag1.remove(itemStr)
print("bag 1 = " + str(bag1))
print()
else:
print("item not in bag")
print()
# Make a copy of bag1 and call it bag2
print("bag 1 = " + str(bag1))
bag2 = bag1.toList()
print("bag 2 = " + str(bag2))
print()
# Check if bag2 contains all of the items in bag1
if bag2.containsAll(bag1):
print("bag 1 contains all elements in bag 2.")
print()
else:
print("bag 1 does not contain all elements in bag 2.")
print()
# Remove 6 from bag2
bag2.remove(6)
print("bag 1 = " + str(bag1))
print("bag 2 = " + str(bag2))
#Check if bag2 contains all of the items in bag1
if bag2.containsAll(bag1):
6. print("bag 1 contains all elements in bag 2.")
print()
else:
print("bag 1 does not contain all elements in bag 2.")
print()
# Check if bag1 contains all of the items bag2
if bag1.containsAll(bag2):
print("bag 2 contains all elements in bag 1.")
print()
else:
print("bag 2 does not contain all elements in bag 1.")
print()
# add testcase for the methods that you add, below
Solution
# An implementation of a Bag Class using a list.
from random import randrange
class ListBag:
# Default, no-arg constructor - creates a new, empty ArrayBag with
# the default maximum size.
def __init__ (self):
self.items = None
self.numItems = 0
# add - adds the specified item to the Bag.
def add(self, item):
if item is None:
print("Added item is illegal")
else:
if self.items is None:
self.items = []
self.items.append(item)
7. self.numItems += 1
else:
self.items.append(item)
self.numItems = self.numItems + 1
# remove - removes one occurrence of the specified item (if any)
# from the Bag. Returns true on success and false if the
# specified item (i.e., an object equal to item) is not in the Bag.
def remove(self, item):
for i in range(self.numItems):
if self.items[i] == item:
self.items.remove(item)
self.numItems = self.numItems - 1
return True
return False # item not found
# contains - returns true if the specified item is in the ListBag, and
# false otherwise.
def contains(self, item):
return item in self.items
# containsAll - does this ListBag contain all of the items in
# otherBag? Returns false if otherBag is null or empty.
def containsAll(self, otherBag):
if otherBag is None or otherBag.numItems == 0:
return False
other = otherBag.toList()
for i in range(len(otherBag.items)):
if not self.contains(otherBag.items[i]):
return False
return True
# numOfItems - returns the number of items in the Bag.
def numOfItems(self):
return self.numItems
# grab - returns a reference to a randomly chosen in the Bag.
def grab(self):
8. if self.numItems == 0:
print("the bag is empty")
return None
whichOne = randrange(0, self.numItems)
return self.items[whichOne]
# toList - return an list containing the current contents of the bag
def toList(self):
copy = ListBag()
for i in range(self.numItems):
copy.add(self.items[i])
return copy
# __str__ - converts this ListBag into a readable string.
def __str__(self):
s = "{"
for i in range(self.numItems - 1):
s = s + " " + str(self.items[i]) + ","
s = s + " " + str(self.items[-1]) + " }"
return s
# isEmpty - add method description
def isEmpty(self):
pass
# remove the pass statement and add you solution
# numOccur - add method description
def numOccur(self, item):
pass
# remove the pass statement and add you solution
# addItems - add method description
def addItems(self, other):
pass
# remove the pass statement and add you solution
# equals - add method description
def equals(self, item):
pass
# remove the pass statement and add you solution
9. # unionWith - add method description
def unionWith(self, item):
pass
# remove the pass statement and add you solution
# Test the ListBag implementation.
# Create an ListBag named bag1.
bag1 = ListBag()
bag1.add(None)
# Determine size of bag1
size = eval(input("Size of bag 1: "))
for i in range(size):
bag1.add(i)
# Print bag1
print("bag 1 = " + str(bag1))
print()
# Print number of items in bag1
print("bag 1 has " + str(bag1.numOfItems()) + " items.")
# Select a random item and print it.
item = bag1.grab()
print("grabbed " + str(item))
print("bag 1 = " + str(bag1))
# Iterate over the objects in bag1, printing them one per line.
for i in bag1.items:
print(i)
print()
# Get an item to remove from bag1, remove it, and reprint the bag.
itemStr = int(input("item to remove: "))
if bag1.contains(itemStr):
bag1.remove(itemStr)
print("bag 1 = " + str(bag1))
print()
else:
10. print("item not in bag")
print()
# Make a copy of bag1 and call it bag2
print("bag 1 = " + str(bag1))
bag2 = bag1.toList()
print("bag 2 = " + str(bag2))
print()
# Check if bag2 contains all of the items in bag1
if bag2.containsAll(bag1):
print("bag 1 contains all elements in bag 2.")
print()
else:
print("bag 1 does not contain all elements in bag 2.")
print()
# Remove 6 from bag2
bag2.remove(6)
print("bag 1 = " + str(bag1))
print("bag 2 = " + str(bag2))
#Check if bag2 contains all of the items in bag1
if bag2.containsAll(bag1):
print("bag 1 contains all elements in bag 2.")
print()
else:
print("bag 1 does not contain all elements in bag 2.")
print()
# Check if bag1 contains all of the items bag2
if bag1.containsAll(bag2):
print("bag 2 contains all elements in bag 1.")
print()
else:
print("bag 2 does not contain all elements in bag 1.")
print()
# add testcase for the methods that you add, below