recursive function

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Jan 8 13:23:36 EST 2007


First possible solution:

def rloop(seqin, comb):
    # xcross product recipe 302478 by David Klaffenbach
    if seqin:
        for item in seqin[0]:
            newcomb = comb + [item]
            for item in rloop(seqin[1:], newcomb):
                yield item
    else:
        yield comb

data = {1: [(3, 4), (5, 8)],
        2: [(5, 4), (21, 3), (19, 2)],
        3: [(16, 1), (0, 2), (1, 2), (3, 4)]}

print [sol for sol in rloop(data.values(), []) if 24==sum(el[0] for el
in sol)]

Bye,
bearophile




More information about the Python-list mailing list