Question: File: useBag.py Author: Derrf Seitz This program exercises bags. The following files must be in the same folder: abstractcollection . py abstractbag

"""
File: useBag.py
Author: Derrf Seitz
This program exercises bags.
The following files must be in the same folder:
abstractcollection.py
abstractbag.py
arraybag.py
arrays.py
ball.py
linkedbag.py
node.py
"""
#
# Replace with your name.
# Replace any "" comments with your own code statement(s)
# to accomplish the specified task.
# DO NOT CHANGE ANY OTHER CODE.
# IMPORTANT NOTE:
# We can use a simple "for" loop to traverse a collection.
# We will call this "regular iteration" or, for brevity, simply "iteration".
# During a regular iteration, you cannot add or remove an item from a framework collection!
# Breaking this rule will cause an immediate exception to occur.
# The exception will display a message and terminate (crash) your program!
# The displayed message is: "Illegal modification of backing store".
# Note that you can update item attributes during iteration.
# Later in this course, we will learn about special "list iterators".
# Unlike regular iterators, list iterators allow modification (add and remove) during iteration.
from arraybag import ArrayBag
from ball import Ball
from linkedbag import LinkedBag
from node import Node
# 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. Every red ball is inflated
# to twice its radius before being added to its new bag.
#
# Preconditions:
# bag - an ArrayBag containing zero or more red and blue Ball objects.
#
# Postconditions:
# returns - a bag containing the doubled radius 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):
#
# Part 3:
# This function removes duplicate items from a bag.
# Postconditions:
# Any item that appears more than once will be reduced to a single occurrence.
# Example: If there were 3 of item A, there will only be 1 of item A remaining.
def removeDuplicates(bag):
#
# Test 1:
print("Test 1:")
bag1= ArrayBag([Ball("red",10),
Ball("red",11),
Ball("red",12),
Ball("blue",15),
Ball("blue",15),
Ball("blue",15)])
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:")
bag2= ArrayBag([Ball("red",201),
Ball("red",201),
Ball("red",202),
Ball("red",202)])
print("Original mixed bag:")
printBag(bag2)
redBag, blueBag = distributeBag(bag2)
print("Red bag:")
printBag(redBag)
print("Blue bag:")
printBag(blueBag)
print("Final mixed bag:")
printBag(bag2)
# Test 3:
print("Test 3:")
bag3= ArrayBag()
print("Original mixed bag:")
printBag(bag3)
redBag, blueBag = distributeBag(bag3)
print("Red bag:")
printBag(redBag)
print("Blue bag:")
printBag(blueBag)
print("Final mixed bag:")
printBag(bag3)
# Test 4:
print("Test 4:")
bag4= LinkedBag(["apple",
"apple",
"banana",
"kiwi",
"cantaloupe",
"pear",
"banana",
"orange",
"orange",
"cantaloupe",
"apple",
"lemon",
"lime",
"lime"])
print("Original bag with duplicates:")
printBag(bag4)
removeDuplicates(bag4)
print("Bag after removing duplicates:")
printBag(bag4)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!