Recursion, generate all pyramid-paths, not working

process circularfunc at gmail.com
Sat Oct 4 10:17:41 EDT 2008


http://projecteuler.net/index.php?section=problems&id=18


def recur(tree, pos):
    if not tree:
        return []
    else:
        return [[tree[0][pos]] + recur(tree[1:], pos)] + \
               [[tree[0][pos]] + recur(tree[1:], pos+1)]



i have a list with [[1],[2,3],[4,5,6],[7,8,9,10]]

it i a pyramid so first is 1 then 23 then 456 thrn 78910

from one can go to 2 or 3. from 2 to 4or5, from 3 to 5 or 6.

i want to generate every path and compute the cost.

But I can't get the recursion right. It generates all the paths kind
of but not in a matter i want to.

also having to return after each other doesnt do anything right?
like so:
return [[tree[0][pos]] + recur(tree[1:], pos)]
return [[tree[0][pos]] + recur(tree[1:], pos+1)]




More information about the Python-list mailing list