[Tutor] for x in myClass
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Sep 2 01:40:18 EDT 2003
Hi Brian,
>> Your second array, called the tag array, would start out unsorted as:
>>
>> [0, 1, 2, 3]
>>
>> Then sorted it would be:
>>
>> [2, 0, 3, 1]
>
> Ok, this looks like the sort is preceding in decending order. Just
> wanted to raise that point, since most references to "sorting" assume
> that we want things in ascending (or more correctly, "nondecending")
> order.
I'm totally, completely wrong here. I don't know what the heck I was
thinking. The sort above is definitely nondecending, not decending.
Sorry about that.
> Somehow, though, I don't think the original assigment anticipated that
> sort() was available for use... *grin*
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]
###
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*
More information about the Tutor
mailing list