help with silly algorhythm
gerson.kurz at t-online.de
Fri Feb 15 13:46:03 EST 2002
On Fri, 15 Feb 2002 11:24:24 -0800, Jeff Shannon <jeff at ccvcorp.com>
>> An ideal job for lambda! Lets start with the data.
>> > * 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]
>> input_list = range(12)
>> partner_values = [1,3,7,10]
>> > 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]....
>> GetUpper = lambda i,p:filter(lambda x:x,[(y,(filter(lambda
>> x:x>y,p)+[None])) for y in i])
>> In typical Python fashion the code is obvious.
>Heh. I positively cannot make heads or tails of that lambda--perhaps its a
>shortcoming of mine, but extended lambdas like that make my eyes glaze and my
>brain shut off....
OK, my post was meant as a joke. Besides, it became clear to my that I
assumed the lists were ordered in advance. Here is, in explicit
notation, what the above lambda expression does, and how.
result = 
# this is the [for y in i] array
for y in i:
# this is the innermost lambda
# will return a list, where each
# element is greater than the current y
temp = filter( IsGreaterThanY, p )
# make sure the list has at least one element
# (namely "None")
temp = temp + [None]
# return the lowest of that list = the
# lowest element that is higher than y
temp = temp
# this is done by the filter x: it will
# remove all elements, where x is None
result.append( (y, temp) )
GetLower() looks similar, but for two changes:
- IsGreaterThanY is replaced by IsLowerThanY, with obvious
- the last element of the temp list is choosen (=the highest element
that is lower than y)
GetLowerWrap() looks like normal GetLower(), only instead of adding
"None" elements and filtering them, the highest value in the list is
choosen (and not filtered).
Mix() uses zip() to create a list of result tuples, and then a simple
xrange to choose odd/even members of that list.
More information about the Python-list