Question: def trimToSize(self): ''' reduce capacity to current size if there is excess capacity Postcondition: Capacity is reduced to current number of items in bag or

def trimToSize(self): ''' reduce capacity to current size if there is excess capacity

Postcondition: Capacity is reduced to current number of items in bag or left unchanged if capacity equals to number of items in bag

Raises: MemoryError if not enough dynamic memory to support additional allocation

''' if self.__used < len(self.__data): if self.__used <= 1: newCapacity = 1 else: newCapacity = self._used # end if # STUDENT WORK HERE # must create a new smaller array with capacity # newCapacity, copy all the items from self.__data # into the new array, and then update the reference # self.__data to refer to the new array

# end if

# end trimToSize

def insert(self, newEntry): ''' adds newEntry to the DoubleArrayBag if capacity available;

Precondition: newEntry must be a double; otherwise, method raises a TypeError exception.

Postcondition: newEntry is placed in Bag just after items already existing and self.__used is incremented by 1.

Args: newEntry (float)

Raises: MemoryError is bag is full

''' if not (isinstance(newEntry, float) or (isinstance(newEntry, int))): raise TypeError("newEntry must be a double") # end if # STUDENT WORK HERE # must ensure that we have the capacity to hold the # newly inserted item -- if not double the current # capacity; then do a normal insertion

# end add

def __iadd__(self, addend): ''' an implementation of the += operation

Precondition: addend is an instance of DoubleArrayBag and is not None

Postcondition: the elements from addend have been added to this bag

Raises: MemoryError if not enough memory available to accommodate operation

TypeError if added is None or is not an instance of DoubleArrayBag ''' if (addend is None) or not(isinstance(addend, self.__class__)): raise TypeError("addend is None or not instance of DoubleArrayBag") # end if # STUDENT WORK HERE # guarantee that we have exactly enough room to hold all the items # from addend and then copy them into this bag; then update any # attributes to ensure that we are maintaining the class # invariant

return self # end plusEqual

def union(b1, b2): ''' this method combines all the elements in this bag and all the elements in the other bag by placing them in a new bag

Also note that this is a class method NOT an instance method

Precondition: b1 and b2 must be an instance of DoubleArrayBag otherwise TypeError is raised

Postcondition: a new DoubleArrayBag is created containing copies of all the elements in b1 plus all the elements in b2.

Args: b1 and b2 (DoubleArrayBag)

Raises: TypeError if b1 or b2 not an instance of DoubleArrayBag

MemoryError if note enough dynamic memory to handle the union '''

if not(isinstance(b1, DoubleArrayBag)): raise TypeError("b1 not instance of DoubleArrayBag") # end if if not(isinstance(b2, DoubleArrayBag)): raise TypeError("b2 not instance of DoubleArrayBag") # end if

# STUDENT WORK HERE # We must create (and ultimately return) a new bag that has exactly # the capacity needed to hold all items from both b1 and b2 and # then copy the items from b1 and then b2 into the new bag -- caution, # watch out when both bags are empty -- be sure to test this

# end union

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!