[Tutor] comparing seqs by successive intervals

kevin parks kp8 at mac.com
Mon Jan 19 13:57:12 EST 2004


Karl's code is really cool and works well, but as Daniel points out, it 
doesn't accommodate sequences
of different length as Don's does. Don's function will traverse both 
lists as long as the shortest
one, ignoring the remainder of the longer list by using:

     numPlaces = min(len(int1),len(int2))
     if int1[:numPlaces] == int2[:numPlaces]:

That's actually the behavior that i want. But really like this idea of 
traversing the sequences
in parallel. I wonder if the benefits of both can be merged....

Thanks Karl (and Don, and Danny, and Daniel...)

Back to work,

kevin


> In that algorithm, where does it check to make sure
> that one of the arrays isn't longer than the other?
> They have to be the same length or else the real
> application of seeing if they are transpositions of
> eachother will not work.
>
>
> --- Karl Pflästerer <sigurd at 12move.de> wrote:
>> On 19 Jan 2004, kevin parks <- kp8 at mac.com wrote:
>>
>>> Actually I am doing something similar to this in
>> some other code. I
>>> have something that removes duplicates and sorts
>> the items beforehand,
>>> then normalizes as above. And of course
>> normalizing will show that
>>> there are indeed transpositions (i have code that
>> does this too) but i
>>> was intrigued and puzzled by this idea of
>> measuring the intervals
>>> between successive list items and wanted to see
>> how that could be done
>>> and then compared to another list, because i
>> eventually find a way to
>>> see how far away the transposed items are....
>> eventually.
>>
>> Here is a different approach; it traverses the
>> sequences in parallel;
>> you will first see a difference if you have long
>> sequences (then mine
>> approach might be a bit faster).
>>
>> def same_intervp(seq1, seq2):
>>     i = 0
>>     res = []
>>     while True:
>>         try:
>>             d1 = seq1[i+1] - seq1[i]
>>             d2 = seq2[i+1] - seq2[i]
>>         except IndexError:
>>             return res
>>         else:
>>             if d1 == d2:
>>                 res.append(d1%12)
>>                 i += 1
>>             else:
>>                 return False
>>




More information about the Tutor mailing list