472,122 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Recursion, generate all pyramid-paths, not working

http://projecteuler.net/index.php?se...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)]

Oct 4 '08 #1
1 1452
process wrote:
http://projecteuler.net/index.php?se...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)]
The backslash is not needed here or anytime there is an open (,[,or {.
Note that tree[0][pos] unless -n <= pos < n where n = len(tree)
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.
I do not understand what function you are trying to compute from the
verbal description. It is generally best to give a specific example(s)
of what output you expect, as well as what you got.
recur([]) = []
recur([[1]],pos) = ? do you start with pos = 0?

What do you mean by 'kind of' and 'not in a manner I want'? We are not
mind (want) readers ;-).
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)]
Useless. The second line is never executed.

tjr

Oct 4 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
10 posts views Thread by Christopher T King | last post: by
8 posts views Thread by virtualadepts | last post: by
reply views Thread by prabhjots | last post: by
35 posts views Thread by Muzammil | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.