[Tutor] A bit long, but would appreciate anyone's help, if time permits!

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sat Jul 24 21:05:00 CEST 2004


Hee-Seng Kye wrote:

> The following is the output of a function I wrote.  The first 6 lines 
> are all possible rotations of [0,1,3,6,7,10], and this takes care of 
> step 1 mentioned above.  The last line provides the differences (mod 
> 12).  If the last line were denoted as r, r[0] lists the differences 
> from first to last element of each rotation (p0 through p5), r[1] the 
> differences from first to second-to-last element, and so on.

I have the impression that the task gets much simpler when the 
differences are stored differently. I would store all the differences 
from the first rotation in r[0], all the differences from the second 
rotation in r[1], etc.

>  >>> from normal import normal
>  >>> normal([0,1,3,6,7,10])
> [0, 1, 3, 6, 7, 10]    #p0
> [1, 3, 6, 7, 10, 0]    #p1
> [3, 6, 7, 10, 0, 1]    #p2
> [6, 7, 10, 0, 1, 3]    #p3
> [7, 10, 0, 1, 3, 6]    #p4
> [10, 0, 1, 3, 6, 7]    #p5
> 
> [[10, 11, 10, 9, 11, 9], [7, 9, 9, 7, 8, 8], [6, 6, 7, 6, 6, 5], [3, 5, 
> 4, 4, 5, 3], [1, 2, 3, 1, 3, 2]]     #r

That would become (when using tuples instead of lists, but that's not 
important):

[(10, 7, 6, 3, 1), (11, 9, 6, 5, 2), (10, 9, 7, 4, 3), (9, 7, 6, 4, 1), 
(11, 8, 6, 5, 3), (9, 8, 5, 3, 2)]

A simple sort() will put the element you want in the first spot, unless 
there's something I'm missing. Problem solved!

Just add this to the end of normal(s):

     transposed = zip(*v)
     print '\n', transposed
     withindices = zip(transposed, range(len(transposed)))
     result = min(withindices)[1]
     print '\n\n', result, r[result]

(I added the indices to keep track of which element got selected by min())

Result is:

------begin------
[0, 1, 3, 6, 7, 10]
[1, 3, 6, 7, 10, 0]
[3, 6, 7, 10, 0, 1]
[6, 7, 10, 0, 1, 3]
[7, 10, 0, 1, 3, 6]
[10, 0, 1, 3, 6, 7]

[[10, 11, 10, 9, 11, 9], [7, 9, 9, 7, 8, 8], [6, 6, 7, 6, 6, 5], [3, 5, 
4, 4, 5, 3], [1, 2, 3, 1, 3, 2]]

[(10, 7, 6, 3, 1), (11, 9, 6, 5, 2), (10, 9, 7, 4, 3), (9, 7, 6, 4, 1), 
(11, 8, 6, 5, 3), (9, 8, 5, 3, 2)]


3 [6, 7, 10, 0, 1, 3]
--------end-------

which I think is what you're looking for.

Instead of transposing via zip(*v) it might also be possible to generate 
the data in that way in the first place.

-- 
"Codito ergo sum"
Roel Schroeven


More information about the Tutor mailing list