[Tutor] putting a short list of integers into a (specific) compact
form
kevin parks
kp8 at mac.com
Sun Oct 12 03:20:15 EDT 2003
Hi everyone,
I've got a question. It is a bit of a brain teaser for me and i am
trying to figure out nice and pythonic way of going about things, but i
just can't get my head round the problem so after staring and the
screen for an eternity, i though that i would give a shout out and see
if i can understand what comes back in response and hopefully i will
have an 'a-ha' type moment. It is quite a complex process for my small
brain so half the battle is understanding the steps and the other is
figuring out how to code it up. As a way to help me get the problem
straight in my pea-sized brain, i though that i would write out the
process and now, since i am stuck, if anyone is bored, i would love
some help, since this is actually being done for a purpose (in order to
get some other work done).
here's what i'd like to do (it is quite complex, but it have tried to
present the recipe as
clearly as possible):
Take a short list of small integers and put it into a very specific
compact form like so:
1. Initially, all duplicate elements should be eliminated and the
elements should
be written so that they are ascending in order (all integers are
mapped mod 12).
2. Since there will be as many ways to order them as there are elements
in
the list, we want to choose the ordering that has the
smallest interval from lowest element to highest one.
3. If there is still a tie under rule #2 we want to pick the list that
is
most packed to the left (by finding the intervals between the first
element
and the second to last element and comparing the intervals of each
list in
the tie, if there is still a tie, compare the intervals between first
element
in the lists and the third to last, etc.)
4. If this still results in a tie for each element, for example in the
case of:
[10,1,6] , [1,6,10] , and [6,10,1] we choose [1,6,10], since
it's first element is 1.
(finally, and optionally, i'll likely need to transpose the answer so
that
it starts on 0)
So in a set with the following five possible orderings (here already
sorted low to high
and proceeding systematically (again %12)):
[0,4,8,9,11]
[4,8,9,11,0]
[8,9,11,0,4]
[9,11,0,4,8]
[11,0,4,8,9]
So the interval between the first element and the last is
[0,4,8,9,11] = 0-11 = 11
[4,8,9,11,0] = 0-4 = 12-4 = 8
[8,9,11,0,4] = 4-8 = 16 - 8 = 8
[9,11,0,4,8] = 9-8 = 20-9 = 11
[11,0,4,8,9] = 9-11 = 21-11 = 10
Here end up with a tie between the second and third orderings above. So
we go to
rule #3: We compare the intervals btween the first and next to last
elements:
second ordering: 11-4 = 7
third ordering: 0-8 = 4
Since 4 is smaller than 7 we conclude the third ordering [8,9,11,0,4]
is more packed to the left and
what we want and there is no need for rule 4 in this case.
I hope that i have stated the question clearly. I would appreciate any
help folks could give on this. Frankly it is beyond
what i can do at this point, but i would like to see how others
approach this in the hopes that my code will work and that
i might learn something.
Best,
kevin
--^----------------------------------------
kp8 @ mac . com
More information about the Tutor
mailing list