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:
abstractcollectionpy
abstractbagpy
arraybag.py
arrays.py
ball.py
linkedbag.py
node.py
#
# Replace with your name.
# Replace any comments with your own code statements
# 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 :
# 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 distributeBagbag:
redBag ArrayBag
blueBag ArrayBag
# Move the balls to the appropriate bags:
#
# Return the bags:
return redBag blueBag
# Part :
# This function prints the items in a bag, each on a separate line.
def printBagbag:
#
# Part :
# 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 of item A there will only be of item A remaining.
def removeDuplicatesbag:
#
# Test :
printTest :
bag ArrayBagBallred
Ballred
Ballred
Ballblue
Ballblue
Ballblue
printOriginal mixed bag:"
printBagbag
redBag, blueBag distributeBagbag
printRed bag:"
printBagredBag
printBlue bag:"
printBagblueBag
printFinal mixed bag:"
printBagbag
# Test :
printTest :
bag ArrayBagBallred
Ballred
Ballred
Ballred
printOriginal mixed bag:"
printBagbag
redBag, blueBag distributeBagbag
printRed bag:"
printBagredBag
printBlue bag:"
printBagblueBag
printFinal mixed bag:"
printBagbag
# Test :
printTest :
bag ArrayBag
printOriginal mixed bag:"
printBagbag
redBag, blueBag distributeBagbag
printRed bag:"
printBagredBag
printBlue bag:"
printBagblueBag
printFinal mixed bag:"
printBagbag
# Test :
printTest :
bag LinkedBagapple
"apple",
"banana",
"kiwi",
"cantaloupe",
"pear",
"banana",
"orange",
"orange",
"cantaloupe",
"apple",
"lemon",
"lime",
"lime"
printOriginal bag with duplicates:"
printBagbag
removeDuplicatesbag
printBag after removing duplicates:"
printBagbag
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
