help with silly algorhythm

kevin parks kp87 at lycos.com
Fri Feb 15 06:55:27 CET 2002


I have been wanting to ask this one for a while, but it is a bit
tricky to articulate so I will try my best.

What I want to do is generate a new list of items based on
an input list and a some criteria (list of acceptable values, for
example). So, i might have:

* list of input values =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 
* a list of acceptable new 'partner values' = [1, 3, 7, 10]

Now I want to create a new list made up of the closest 
members of the [1, 3, 7, 10] list in 4 different ways:
  - upper
  - lower
  - alternating starting higher
  - alternating starting with closest lower value

All with the further stipulation that values from the input list
do not team with values in the "buddy" list if they are the
same, so input value 1 would not go with 1 from [1, 3, 7, 10], 
but with 3 (the next value up),  or 10 (next value down), depending
on which of the 4 possible 'modes' we chose.

vague i know, i am trying my best...

so we might get (in mode 1: all closest higher value)

[ [0, 1], [1, 3], [2, 3], [3, 7], [4, 7], [5, 7], [6, 7], [7, 11]....

-or (as a single new list) -

[1, 3, 3, 7, 7, 7, 7, 11.....]

mode 2 (all closest value down) might be:

[[0,10], [1, 10], [2, 1], [3, 1], [4, 3], [5,3], [6,3], [7,3], [8, 7]....

mode 3

[ [0, 1], [1, 10], [2, 3], [3, 1].....

etc (hope i haven't made a mistake here).

So problem number one for me is how do i figure out which value
is 'closest' (up or down), do i have to test it against each
value of the list or is there a smarter way.

I realize that this is very odd and convoluted. If anyone can make
heads or tails of this explanation and would like to help me I
would be grateful.

-kp--



More information about the Python-list mailing list