If Python did not already have a List data type, you might need to implement a data structure such as the dynamic array or vector described in this chapter. In this exercise you will explore how this can be done. Begin by creating the following class defi nition: class Vector (object): def __init__ (self): self.data = [ None ] * 5 self.size = 0 self.capacity = 5 def add (self, newValue): # add an element to the collection pass def insert (self, I, newValue): # insert at location i pass def remove (self, i): # remove value at location i pass def test (self, testValue): # see if collection contains element pass def __iter__(self): return self.data.__iter__() def size (self): return self.size Implement the functions add and insert. The add function places the new element at the end of the collection, while insert places the new element at a specifi c point. Both functions need to check that the size does not exceed the capacity before placing the value. If it does, the size of the array stored in self.data should be doubled and the capacity modifi ed appropriately. The remove method should delete an element at the given location. This reduces the size, but not the capacity. The test method should see if the value is held in the collection.
#Sales Offer!| Get upto 25% Off: