Question: 5.5) Can you please help me with this Python question. The clone() method expects no arguments when called, and returns an exact copy of the
5.5) Can you please help me with this Python question.
The clone() method expects no arguments when called, and returns an exact copy of the type of bag on which it is called.
# Could not post 5.4 Solution because of length problem. i will post on comment if needed
In the arraybag.py file complete the following:
- Define the clone() method to the ArrayBag class.
- Returns a copy of self.
In the linkedbag.py file complete the following:
- Define the clone() method to the LinkedBag class.
- Returns a copy of self.
For example, the variable bag2 would contain the numbers 2, 3 and 4 at the end of the following code segment:
bag1 = ArrayBag([2,3,4]) bag2 = bag1.clone() bag1 == bag2 # Returns True bag1 is bag2 # Returns False
File: arraybag.py
"""
Project 5.5
File: arraybag.py
Reuse your solution from Programming Exercise 5.4 as your starter file
"""
from arrays import Array
class ArrayBag(object):
"""An array-based bag implementation."""
# Reuse your solution from Programming Exercise 5.4 as your starter file
# 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)
self.size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise."""
return len(self) == 0
def __len__(self):
"""Returns the number of items in self."""
return self.size
def __str__(self):
"""Returns the string representation of self."""
return "{" + ", ".join(map(str, self)) + "}"
def __iter__(self):
"""Supports iteration over a view of self."""
cursor = 0
while cursor < len(self):
yield self.items[cursor]
cursor += 1
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
result = ArrayBag(self)
for item in other:
result.add(item)
return result
def __eq__(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(item) != other.count(item):
return False
return True
def count(self, item):
"""Returns the number of instances of item in self."""
total = 0
for nextItem in self:
if nextItem == item:
total += 1
return total
# Mutator methods
def clear(self):
"""Makes self become empty."""
self.size = 0
self.items = Array(ArrayBag.DEFAULT_CAPACITY)
File: linkedbag.py
"""
Project 5.5
File: linkedbag.py
"""
from node import Node
class LinkedBag(object):
"""A link-based bag implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self.items = None
self.size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise."""
return len(self) == 0
def __len__(self):
"""-Returns the number of items in self."""
return self.size
def __str__(self):
"""Returns the string representation of self."""
return "{" + ", ".join(map(str, self)) + "}"
def __iter__(self):
"""Supports iteration over a view of self."""
cursor = self.items
while not cursor is None:
yield cursor.data
cursor = cursor.next
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
result = LinkedBag(self)
for item in other:
result.add(item)
return result
def clone(self):
"""Returns a copy of self."""
def __eq__(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 not item in other:
return False
return True
# Mutator methods
def clear(self):
"""Makes self become empty."""
self.size = 0
self.items = None
def add(self, item):
"""Adds item to self."""
self.items = Node(item, self.items)
self.size += 1
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 node containing the target item
# probe will point to the target node, and trailer
# will point to the one before it, if it exists
probe = self.items
trailer = None
for targetItem in self:
if targetItem == item:
break
trailer = probe
probe = probe.next
# Unhook the node to be deleted, either the first one or one
# thereafter
if probe == self.items:
self.items = self.items.next
else:
trailer.next = probe.next
# Decrement logical size
self.size -= 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
