On 03/04/2015 10:40 AM, niyanax...@gmail.com wrote:
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.


I don't know any Python that includes something called ezarrays in its library. What version of Python are you using, and where do you get ezarrays?

In general, the tutor list is for learning about Python and its standard library. You might get lucky, and find someone who knows your special library, but that's more likely on either python-list, or in a forum that is specifically for that library.

We'll try to help here, but it'll be based on a bunch of guesses, rather than experience. Not the best way to get help.

I'm going to speculate and guess you're talking about
    http://courses.necaiseweb.org/EZArrays/EZArrays



Assignment:

Seems like it's trying to teach you pascal, or java, not Python.


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.

You called the method __len__(), rather than the requested length().
It's not clear what they want here, but it's a reasonable guess that they want 1+ the index of the highest item that isn't None.


• 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.

Again, you implemented __getitem__() rather than the requested getitem()

• 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.

And again here.

• contains(item): Determines if the given item is contained in the vector.

And again here.


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

But you called it __str__


• 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.


Not clear if this is supposed to be symmetric with insert. If so, then you have to slide things left, like you slid them right in the previous method.

• 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."

assert should never be used to check data values, but only for program invariants. It should be an if statement with a raise statement in the body. Same thing for other asserts in this code.

     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)

   At this point, the _numItems value is incorrect

     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

Don't use a while loop when you know the range it'll be using. And don't use a range when you can iterate directly over the collection. A for loop would be half the size


     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) :

Seems like the easiest way would be to simply call setitem with the appropriate index value.

     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)

No need to do that. Just set the _numItems to zero. Nobody's allowed to look beyond that anyway.


    # 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) :

You need to check both that the index is within range, and that _numItems isn't already at max. Do a raise if either situation occurs.

     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) :

You need to test the value of index, to see if it's within range.

     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

You need to raise an exception to tell the caller that the item was not in the list.



    # 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

Generally, it's better practice to use an expression instead of another variable. The end value is always _numItems - x -1

And you can swap two items just by doing a tuple assignment:

       a, b = b, a


--
DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to