0 abstractbag.pyx abstractcollection.py |redistributeBag.py rearraybag.pyxl?narr
ID: 3708311 • Letter: 0
Question
0
abstractbag.pyx abstractcollection.py |redistributeBag.py rearraybag.pyxl?narrays.py, eball.py from abstractcollection import AbstractCollection AbstractBag(AbstractCollection) : """Repository for common bag data and methods. " class 9 10 # Constructor def init (self, sourceCollection= None) : 12 13 14 15 Sets the initial state of self, which includes the contents of sourceCollection, if it's present." AbstractCollection.--init--(self, sourceCollection) 17 18 19 20 ot 21 # Accessor methods def tr (self): "Returns the string rep of the bag.""" result AbstractCollection. str (self) return "result [1:-1"" 24 25 o 26 27 28 29 30 31 32 def _ecg (self, other): """Returns True if self equals other, or False otherwise. if self is other: return True if type (self) != type (other) or len (self) len (other) return False for item in self: if self.count (1tem) return False != other.count (1tem): 34 35 3 6 return TrueExplanation / Answer
PROGRAM:
from arrays import Array//FROMTHEARRAY LIST
class ArrayBag(object)://ALSO FROM ARRAY
DEFAULT_CAPACITY=10// DEFAULT VALUEOF 10
def __init__(self, sourceCollection = None)://THERE ISNO SOURCE COLLLECTION
self._items = Array(ArrayBag.DEFAULT_CAPACITY)
self._size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
def isEmpty(self):
return len(self) == 0
def __len__(self):
return self._size
def __str__(self):
return "{" + ", ".join(map(str, self)) + "}"
def clear(self):
self._size = 0
self._items = Array(ArrayBag.DEFAULT_CAPACITY)
def add(self, item):
self._items[len(self)] = item
self._size += 1
def remove(self, item):
self._size -= 1
class Ball:
DEFAULT_RADIUS = 1
def __init__(self, color, radius = 1):
self.color = color
self.radius = radius
def __eq__(self, other):
if self is other:
return True
if (type(self) != type(other)) or
(self.color != other.color) or
(self.radius != other.radius):
return False
return True
def __str__(self):
return (self.color + " ball, radius " + str(self.radius))
def getColor(self):
return self.color
def getRadius(self):
return self.radius
def setColor(self, color):
self.color = color
def setRadius(self, radius):
self.radius = radius
def distributeBag(bag):
redBag = ArrayBag()
blueBag = ArrayBag()
for x in bag:
if(x.getColor == "red"):
redBag.add(x)
elif(x.getColor=="blue"):
blueBag.add(x)
bag.remove(x)
return (redBag, blueBag)
def ArrayBag(mylist = [], *args):
bag = []
for i in mylist:
bag[i]=mylist
return bag
def printBag(bag):
for x in bag:
str(x)
Test 1:
print("Test1:")
bag1 = ArrayBag([Ball("red", 1),Ball("red", 2),Ball("red", 3),Ball("blue", 1),
Ball("blue"),Ball("blue", 3)])
print("Original mixed bag:")
printBag(bag1)
redBag, blueBag = distributeBag(bag1)
print("Red bag:")
printBag(redBag)
print("Blue bag:")
printBag(blueBag)
print("Final mixed bag:")
printBag(bag1)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.