Need help trying to implement insert, remove, indexof, and reverse functions. 

I tried to do them but am not sure if it is correct. I am struggling with 
arrays.


This is python and using ezarrays. 


Assignment:

A fixed vector is very similar to the vector in that it is a sequence container 
that can grow and shrink as needed and include many useful operations. But the 
fixed vector has a maximum capacity beyond which it can not expand. 

• FixedVector(maxSize): Creates an empty fixed vector with the given maximum 
capacity. 

• length(): Returns the number of elements contained in the vector. 

• isFull(): Returns a Boolean indicating if the vector is full.

• getitem(index): Returns the element stored in the vector at position index. 
The value of index must be within the valid range. 

• setitem(index, item): Sets the element at position index to contain the given 
item. The value of index must be within the valid range, which includes one 
position beyond the end of the vector. In the latter case, the item is simply 
appended onto the end. • contains(item): Determines if the given item is 
contained in the vector. 

• toString(): Returns a string representation of the vector. 

• append(item): Adds the given item to the end of the vector. An item can not 
be appended to a full vector. 

• clear(): Removes all elements from the vector. 

• insert(index, item): Inserts the given item into the vector at position 
index. The elements at and following the given position are shifted down to 
make room for the new item. The index must be within the valid range and the 
vector can not be full. If index is one position beyond the end of the vector, 
the item is appended onto the vector. 

• remove(index): Removes the element at position index from the vector. The 
removed element is returned. The index must be within the valid range. 

• indexOf(item): Returns the index of the element containing the given item. 
The item must be in the list. 

• reverse(): Performs a list reversal by reversing the order of the elements 
within the vector. 





My Code:


from ezarrays import Array


class FixedVector :
   # Creates a new empty fixed vector with the given maximum capacity.
  def __init__(self, maxSize) :
    self._theItems = Array(maxSize)
    self._numItems = 0
    
   # Returns the number of elements contained in the vector.
  def __len__(self) :
    return self._numItems
    
   # Returns a Boolean indicating if the vector is full.
  def isFull(self) :
    return self._numItems == len(self._theItems)
    
   # Returns the element stored in the vector at position index. 
   # The value of index must be within the valid range. 
  def __getitem__(self, index) :
    assert index >= 0 and index < self._numItems, "Index out of Range."
    return self._theItems[index]
    
   # Sets the element at position index to contain the given item. The 
   # value of index must be within the valid range, which includes one 
   # position beyond the end of the vector. In the latter case, the item 
   # is simply appended onto the end.  
  def __setitem__(self, index, item) :
    assert index >= 0 and index <= self._numItems, "Index out of range."
    
    if index == self._numItems :
      self.append(item)
    else :
      self._theItems[index] = item         
    
   # Determines if the given item is contained in the vector.
  def __contains__(self, item) :
    i = 0
    while i < self._numItems : 
      if self._theItems[i] == item :
        return True
      i = i + 1
      
    return False
  
   # Returns a string representation of the vector.
  def __repr__(self) :
    if self._numItems == 0 :
      return "[]"
      
    vectStr = "[" + str(self._theItems[0])
    for i in range(1, self._numItems) :
      vectStr = vectStr + ", " + str(self._theItems[i])
      
    vectStr = vectStr + "]"
    return vectStr
        
   # Adds the given item to the end of the vector. An item can not be 
   # appended to a full vector.
  def append(self, item) :
    assert self._numItems < len(self._theItems), \
           "Can not add to a full vector."
    self._theItems[self._numItems] = item
    self._numItems = self._numItems + 1
  
   # Removes all elements from the vector.
  def clear(self) :
    self._theItems.clear(None)     
    
   # Inserts the given item into the vector at position index. The elements 
   # at and following the given position are shifted down to make room 
   # for the new item. The index must be within the valid range and the
   # vector can not be full. If index is one position beyond the end of
   # the vector, the item is appended.
  def insert(self, index, item) :
    i = numItems
    while i > index :
        self._theItem[i] = self._theItem[i-1]
        i = i-1 
    self._theItems[i] = item
    self._numItems = self._numItems + 1
  
   # Removes the element at position index from the vector. The removed
   # element is returned. The index must be within the valid range.
  def remove(self, index) :
    removed = self._theItems[index]
    self._theItems[index] = None 
    return removed
    
  
   # Returns the index of the element containing the given item. The item 
   # must be in the list.
  def indexOf(self, item) :
    for i in range(0, self._numItems ):
      if self._theItems[i] == item :
        return i
      
        
   # Reverses the order of the elements within the vector.
  def reverse(self) :
    end = self._numItems -1
    for x in range(0, self._numItems // 2) :
      tmp = self._theItems[x]
      self._theItems[x] = self._theItems[end]
      self._theItems = tmp
      end = end -1
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to