[Tutor] Re: Tutor Digest, Vol 2, Issue 2

Brian Christopher Robinson brian at dungeoncrawl.org
Tue Sep 2 22:35:35 EDT 2003


At 04:50 AM 2/09/03, you wrote:
>It looks like you're doing this just out of curiosity, so I'm not too
>concerned about this being a homework assignment.  But if you are doing
>doing this for homework, please tell us --- we are prohibited from helping
>much with homework.

Actually, a it was a friend's homework assignment but he's completed it 
already and I'm not a student at all.  I just thought it was an interesting 
problem that would let me try out some things in Python.

>Those last two statements can also be written as:
>
>      self.extend(range(len(array)))
>
>extend() is a method that can append multiple elements at a time to a
>list.  One line isn't much, but every bit helps.  *grin*

Ok, thanks.  I put this in.

>In Case 1, you'll want to override the __iter__() method, which Python
>uses to get an iterator when it does a 'for' loop:

Ok, this is what I put as a member of my TagSort class:

     def __iter__(self):
         return self.array.__iter__()

Now, this is actually wrong since it doesn't take in to account the order 
of the TagArray.  But this doesn't even work at all.  Instead I get the 
message:

AttributeError: 'list' object has no attribute '__iter__'

What's that all about?

So then I wrote this:

     def __iter__(self):
         class TagIterator:
             def __init__(self, tagArray):
                 self.tagArray = tagArray
                 self.count = 0

             def __iter__(self):
                 return self

             def next(self):
                 try:
                     value = self.tagArray[self.count]
                 except:
                     raise StopIteration()
                 self.count += 1
                 return value

         return TagIterator(self)

Which appears to work.  I still can't call the sort method on my TagArray 
class, though.


>Iff sort() were available to us, then there's an embarassingly simply way
>to solve this problem.
>
>###
> >>> def create_tag_array(L):
>...     tag_array = range(len(L))
>...     def mycmp(x, y):
>...         return cmp(L[x], L[y])
>...     tag_array.sort(mycmp)
>...     return tag_array
>...
> >>> create_tag_array([1, 50, 0, 42])
>[2, 0, 3, 1]
>###

I tried to do something like this originally, but I couldn't quite get the 
syntax right.  Thanks.

>There are other really silly approaches to generate the sorted tag array,
>and all of the silly approaches involve using sort().  So I'd rather hope
>that the original assignment does not allow sort() at all.  *grin*

It wasn't an assignment about sorting specifically, so I don't see why you 
couldn't use the sort method.

My first attempt was this:

def tagSort(array):
     copy = array[:] # copies the array
     copy.sort()
     indexes = []
     for number in copy:
         indexes.append(array.index(number))
     return indexes

Which works great.  My friend was opposed to the copy, though, so I wrote this:

def tagSort2(array):
     tag = range(len(array)) # creates a list from 0 to array length
     for i in range(len(array)):
         for j in range(len(array[:-(i+1)])):
             if array[tag[j + 1]] < array[tag[j]]:
                 tag[j + 1], tag[j] = tag[j], tag[j + 1]
     return tag

Which just does a bubble sort with the indirection.

I just wanted to pursue the object way because I'd like to be able to write:

def tagSort3(array):
     tag = TagArray(array)
     tag.sort()
     return tag



-- 
reaching out to embrace whatever may come 




More information about the Tutor mailing list