[Tutor] A bit long, but would appreciate anyone's help,
if time permits!
Hee-Seng Kye
kyeser at earthlink.net
Fri Jul 23 16:24:19 CEST 2004
Hi. I have a question that requires a bit of explanation. I would
highly appreciate it if anyone could read this and offer any
suggestions, whenever time permits.
I'm trying to write a program that 1) gives all possible rotations of
an ordered list, 2) chooses the ordering that has the smallest
difference from first to last element of the rotation, and 3) continues
to compare the difference from first to second-to-last element, and so
on, if there was a tie in step 2.
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.
>>> 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
Here is my question. I'm having trouble realizing step 2 (and 3, if
necessary). In the above case, the smallest number in r[0] is 9, which
is present in both r[0][3] and r[0][5]. This means that p3 and p5 and
only p3 and p5 need to be further compared. r[1][3] is 7, and r[1][5]
is 8, so the comparison ends here, and the final result I'm looking for
is p3, [6,7,10,0,1,3] (the final 'n' value for 'pn' corresponds to the
final 'y' value for 'r[x][y]').
How would I find the smallest values of a list r[0], take only those
values (r[0][3] and r[0][5]) for further comparison (r[1][3] and
r[1][5]), and finally print a p3?
Thanks again for reading this. If there is anything unclear, please
let me know.
Best,
Kye
My code begins here:
#normal.py
def normal(s):
s.sort()
r = []
q = []
v = []
for x in range(0, len(s)):
k = s[x:]+s[0:x]
r.append(k)
for y in range(0, len(s)):
print r[y], '\t'
d = []
for yy in range(len(s)-1, 0, -1):
w = (r[y][yy]-r[y][0])%12
d.append(w)
q.append(d)
for z in range(0, len(s)-1):
d = []
for zz in range(0, len(s)):
w = q[zz][z]
d.append(w)
v.append(d)
print '\n', v
More information about the Tutor
mailing list