Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# This program exercises bags: # Replace any comments with your own code statement(s) # to accomplish the specified task. # Do not change

 

# 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:

#

# 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): #

# 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 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

==================================================================================================

#not suer if this is needed: 

""" File: BSTinterface.py Copyright 2015 by Ken Lambert

"""

class BSTInterface(object): """Interface for all binary search trees."""

# Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" pass

# Accessor methods def isEmpty(self): """Returns True if len(self) == 0, or False otherwise.""" return True def __len__(self): """-Returns the number of items in self.""" return 0

def __str__(self): """Returns the string representation of self.""" return ""

def __iter__(self): """Supports a preorder traversal on a view of self.""" return None

def inorder(self): """Supports an inorder traversal on a view of self.""" lyst = list() def recurse(node): if node != None: recurse(node.left) lyst.append(node.data) recurse(node.right) recurse(self._root) return iter(lyst)

def postorder(self): """Supports a postorder traversal on a view of self.""" return None

def levelorder(self): """Supports a levelorder traversal on a view of self.""" return None

def __add__(self, other): """Returns a new bag containing the contents of self and other.""" return None

def __eq__(self, other): """Returns True if self equals other, or False otherwise.""" return False

def __contains__(self, item): """Returns Ture if item is in self, or False otherwise.""" return True

def find(self, item): """If item matches an item in self, returns the matched item, or None otherwise.""" return None

# Mutator methods def clear(self): """Makes self become empty.""" pass

def add(self, item): """Adds item to self.""" pass

def remove(self, item): """Precondition: item is in self. Raises: KeyError if item is not in self. postcondition: item is removed from self.""" pass

def replace(self, item, newItem): """Precondition: item == newItem. Raises: KeyError if item != newItem. If item is in self, replaces it with newItem and returns the old item, or returns None otherwise.""" return None

==================================================================================================Open in Python Editor

Programming Activity 3 - Guidance ================================= Part 1 ====== distributeBag() function ------------------------ Read the function header to understand what this function does. Pay particular attention to its preconditions and postconditions. You need to add your code to this function where indicated. Do not change any of its existing code. Note that this function's return statement returns 2 objects: The red bag and the blue bag objects. Returning multiple objects in a single return statement is a unique Python feature. The balls --------- The balls in the bags are objects of class Ball. This class is provided under the assignment in the course web page. Also provided there is a ball tester, which makes it easy to see how to use a ball. A red ball's color is the string "red". A blue ball's color is the string "blue". Moving the balls ---------------- Moving the balls means they need to end up in the right bag. It also means they should not be in the starting bag after your code completes. For each test case, a reference to the starting bag is passed in as a function parameter. Changes you make to this bag parameter result in changes to the bag used at the point of call. This is because parameters in Python are passed by reference, rather than by copied value. Your code should be general enough to handle all 3 sets of test data. Do NOT write 3 separate blocks of code, one for each specific test set. Instead, your single solution should work for any of these test cases. You should use a for loop on the original mixed bag. You do NOT need to remove the balls from the mixed bag as you see them. You can wait until after all the ball references have been added to the unicolor bags. Using references is a shallow copy of the balls. This is OK here because you will be removing the ball references from the mixed bag. This means there will not be any duplicate references in differebt bags to worry about. Keep it simple -------------- Do not overthink/overcomplicate this. Solutions are simple and easy to understand. There is more than one possible coding solution. Part 2 ====== printBag() function ------------------- This function takes a bag as a paramater. Look at the expected output document to see what it is supposed to print. It must iterate through the given bag. Each item must be printed on a separate line. To get a string version of a ball item, just use str(item). Notice that it must print "empty" if the bag is empty. Notice that it must print a blank line after the bag output.

.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

Students also viewed these Databases questions