Question: PLEASE HELP! THIS REQUIRES PYTHON! distributebag.py # This program exercises bags: # Replace any comments with your own code statement(s) # to accomplish the
PLEASE HELP! THIS REQUIRES PYTHON!
distributebag.py
# This program exercises bags:
# Replace any "" comments with your own code statement(s) # to accomplish the specified task. # Do not change any other code.
# The following files must be in the same folder: # abstractcollection.py # abstractbag.py # arraybag.py # arrays.py # ball.py
from arraybag import ArrayBag from ball import Ball
# Part 1: # This function takes a bag that has red and blue balls in it # and moves the balls to two other bags: one to hold the red # balls and one to hold the blue balls. # # Preconditions: # bag - an ArrayBag containing zero or more red and blue Ball objects. # # Postconditions: # returns - a bag containing the red balls and # a bag containing the blue balls. # The original bag should be emptied. def distributeBag(bag): redBag = ArrayBag() blueBag = ArrayBag()
# Move the balls to the appropriate bags:
# yourcode
# Return the 2 bags: return (redBag, blueBag)
# Part 2:
# This function prints the items in a bag, each on a separate line. def printBag(bag): # yourcode
# Test 1: print("Test 1:") bag1 = ArrayBag([Ball("red", 1), Ball("red", 2), Ball("red", 3), Ball("blue", 1), Ball("blue", 2), 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)
# Test 2: print("Test 2:") bag1 = ArrayBag([Ball("red", 1), Ball("red", 2),]) 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)
# Test 3: print("Test 3:") bag1 = ArrayBag() 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)
ball.py
# Ball class
class Ball:
# Class variable DEFAULT_RADIUS = 1
# Constructor: def __init__(self, color, radius = DEFAULT_RADIUS): self.color = color; self.radius = radius;
# Special methods: 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))
# Accessor methods: def getColor(self): return self.color
def getRadius(self): return self.radius
# Mutator methods: def setColor(self, color): self.color = color
def setRadius(self, radius): self.radius = radius
array.py
""" File: arrays.py Copyright 2015 by Ken Lambert
An Array is a restricted list whose clients can use only [], len, iter, and str.
To instantiate, use
= array(, )
The fill value is None by default. """
class Array(object): """Represents an array."""
def __init__(self, capacity, fillValue = None): """Capacity is the static size of the array. fillValue is placed at each position.""" self._items = list() for count in range(capacity): self._items.append(fillValue)
def __len__(self): """-> The capacity of the array.""" return len(self._items)
def __str__(self): """-> The string representation of the array.""" return str(self._items)
def __iter__(self): """Supports iteration over a view of an array.""" return iter(self._items)
def __getitem__(self, index): """Subscript operator for access at index.""" return self._items[index]
def __setitem__(self, index, newItem): """Subscript operator for replacement at index.""" self._items[index] = newItem
arraybag.py
""" File: arraybag.py Copyright 2015 by Ken Lambert
"""
from arrays import Array from abstractbag import AbstractBag
class ArrayBag(AbstractBag): """An array-based bag implementation."""
# Class variable DEFAULT_CAPACITY = 10
# Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" self._items = Array(ArrayBag.DEFAULT_CAPACITY) AbstractBag.__init__(self, sourceCollection)
def __iter__(self): """Supports iteration over a view of self.""" modCount = self.getModCount() cursor = 0 while cursor < len(self): yield self._items[cursor] if modCount != self.getModCount(): raise AttributeError("Illegal modification of backing store") cursor += 1
# Mutator methods def clear(self): """Makes self become empty.""" self._size = 0 self._modCount = 0 self._items = Array(ArrayBag.DEFAULT_CAPACITY)
def add(self, item): """Adds item to self.""" # Resize array if necessary if len(self) == len(self._items): tempArray = Array(len(self) * 2) for i in range(len(self)): tempArray[i] = self._items[i] self._items = tempArray self._items[len(self)] = item self._size += 1 self.incModCount()
def remove(self, item): """Precondition: item is in self. Raises: KeyError if item in not in self. Postcondition: item is removed from self.""" # Check precondition and raise if necessary if not item in self: raise KeyError(str(item) + " not in bag") # Search for the index of the target item targetIndex = 0 for targetItem in self: if targetItem == item: break targetIndex += 1 # Shift items to the left of target up by one position for i in range(targetIndex, len(self) - 1): self._items[i] = self._items[i + 1] # Decrement logical size self._size -= 1 self.incModCount() # Resize array if necessary if len(self) <= .25 * len(self._items) and \ ArrayBag.DEFAULT_CAPACITY <= len(self._items) // 2: tempArray = Array(len(self._items) // 2) for i in range(len(self)): tempArray[i] = self._items[i] self._items = tempArray
**OUTPUT**
Test 1:
Original mixed bag:
red ball, radius 1
red ball, radius 2
red ball, radius 3
blue ball, radius 1
blue ball, radius 2
blue ball, radius 3
Red bag:
red ball, radius 1
red ball, radius 2
red ball, radius 3
Blue bag:
blue ball, radius 1
blue ball, radius 2
blue ball, radius 3
Final mixed bag:
empty
Test 2:
Original mixed bag:
red ball, radius 1
red ball, radius 2
Red bag:
red ball, radius 1
red ball, radius 2
Blue bag:
empty
Final mixed bag:
empty
Test 3:
Original mixed bag:
empty
Red bag:
empty
Blue bag:
empty
Final mixed bag:
empty
I ONLY NEED HELP WITH THE distributionBag.py CODES. THE OTHER FILES ARE THE ONES THAT ARE NEEDED IN ORDER TO MAKE THE FILE RUN. I WILL RATE YOU! THANKS!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
