How does 'filter' and 'reduce' work?

zeallous zeallous at bigpond.net.au
Thu May 15 23:46:00 EDT 2003


Dear python talents!

    I had this question in one of our tests. But I am not quite sure
how to get to the answer:

def f(L):
    if len(L) == 0
        return []
    else
        return f([x for x in L[1:] if x < L[0]]) + \
               [L[0]] + \
               f([x for x in L[1:] if x >= L[0]])

N = 10
print f(filter(lambda x: x % 3 == 0,
               map ( lambda x, y: x+reduce(lambda a,b: a-b, y),
                     range(N), [range(3)]*N)


The answer was: [-3, 0, 3, 6]

Answers from anyone of you would be appreciated!! =)

Thanks!!

zeallous




More information about the Python-list mailing list