Using recursion to build various trees
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to produce a complete range of possible trees, based on a given number of total mass. All trees having a first layer of mass 1, and for all subsequent layers underneath they either maintain the previous layer's mass, or it is increased by 1. (Barring the stem for now.) I figured recursion would lead me to the light, however I seem to have some problems producing more then a thin branch of 1, all the way to the bottom.
After the first pass an error reads:
File "test3.py", line 21, in <module>
for perm in tree(56):
TypeError: 'NoneType' object is not iterable
I'm not sure where it wants to iterate. I have very little experience with recursion, and only in Prolog. This is my program:
def tree(total):
tree = [1]
current = 1
def build_up(total,current):
if total == 1:
tree.append(1)
print(tree)
return 1
elif total <= 0:
return 0
else:
for i in range(current,current+1):
tree.append(i)
current = i
total -= i
build_up(total,current)
build_up(total-1,current)
for perm in tree(56):
print(perm)
python recursion
add a comment |
I'm trying to produce a complete range of possible trees, based on a given number of total mass. All trees having a first layer of mass 1, and for all subsequent layers underneath they either maintain the previous layer's mass, or it is increased by 1. (Barring the stem for now.) I figured recursion would lead me to the light, however I seem to have some problems producing more then a thin branch of 1, all the way to the bottom.
After the first pass an error reads:
File "test3.py", line 21, in <module>
for perm in tree(56):
TypeError: 'NoneType' object is not iterable
I'm not sure where it wants to iterate. I have very little experience with recursion, and only in Prolog. This is my program:
def tree(total):
tree = [1]
current = 1
def build_up(total,current):
if total == 1:
tree.append(1)
print(tree)
return 1
elif total <= 0:
return 0
else:
for i in range(current,current+1):
tree.append(i)
current = i
total -= i
build_up(total,current)
build_up(total-1,current)
for perm in tree(56):
print(perm)
python recursion
When quoting error messages, please indicate which line of your code it applies to.
– AShelly
Jan 4 at 0:12
Your immediate exception is caused by the fact thattreedoens't return anything (which is the same as returningNonein Python). Sofor perm in tree(56)fails, astree(56)isNone, which is not iterable. This is probably not the only problem with your code, but I honestly don't know what you're trying to do, so I can't really help with anything else.
– Blckknght
Jan 4 at 0:55
Changing 'multitude' to 'complete range' probably isn't sufficient disambiguation to get you an answer -- perhaps a rough mock up of expected output would help clarify.
– cdlane
Jan 4 at 17:56
Welcome to SO! I'm voting to close as unclear what's being asked. Please post your expected output and clarify what you mean by "mass", "stem", "branch" and "layer".
– ggorlen
Jan 4 at 22:02
add a comment |
I'm trying to produce a complete range of possible trees, based on a given number of total mass. All trees having a first layer of mass 1, and for all subsequent layers underneath they either maintain the previous layer's mass, or it is increased by 1. (Barring the stem for now.) I figured recursion would lead me to the light, however I seem to have some problems producing more then a thin branch of 1, all the way to the bottom.
After the first pass an error reads:
File "test3.py", line 21, in <module>
for perm in tree(56):
TypeError: 'NoneType' object is not iterable
I'm not sure where it wants to iterate. I have very little experience with recursion, and only in Prolog. This is my program:
def tree(total):
tree = [1]
current = 1
def build_up(total,current):
if total == 1:
tree.append(1)
print(tree)
return 1
elif total <= 0:
return 0
else:
for i in range(current,current+1):
tree.append(i)
current = i
total -= i
build_up(total,current)
build_up(total-1,current)
for perm in tree(56):
print(perm)
python recursion
I'm trying to produce a complete range of possible trees, based on a given number of total mass. All trees having a first layer of mass 1, and for all subsequent layers underneath they either maintain the previous layer's mass, or it is increased by 1. (Barring the stem for now.) I figured recursion would lead me to the light, however I seem to have some problems producing more then a thin branch of 1, all the way to the bottom.
After the first pass an error reads:
File "test3.py", line 21, in <module>
for perm in tree(56):
TypeError: 'NoneType' object is not iterable
I'm not sure where it wants to iterate. I have very little experience with recursion, and only in Prolog. This is my program:
def tree(total):
tree = [1]
current = 1
def build_up(total,current):
if total == 1:
tree.append(1)
print(tree)
return 1
elif total <= 0:
return 0
else:
for i in range(current,current+1):
tree.append(i)
current = i
total -= i
build_up(total,current)
build_up(total-1,current)
for perm in tree(56):
print(perm)
python recursion
python recursion
edited Jan 4 at 17:01
sybren osinga
asked Jan 4 at 0:08
sybren osingasybren osinga
42
42
When quoting error messages, please indicate which line of your code it applies to.
– AShelly
Jan 4 at 0:12
Your immediate exception is caused by the fact thattreedoens't return anything (which is the same as returningNonein Python). Sofor perm in tree(56)fails, astree(56)isNone, which is not iterable. This is probably not the only problem with your code, but I honestly don't know what you're trying to do, so I can't really help with anything else.
– Blckknght
Jan 4 at 0:55
Changing 'multitude' to 'complete range' probably isn't sufficient disambiguation to get you an answer -- perhaps a rough mock up of expected output would help clarify.
– cdlane
Jan 4 at 17:56
Welcome to SO! I'm voting to close as unclear what's being asked. Please post your expected output and clarify what you mean by "mass", "stem", "branch" and "layer".
– ggorlen
Jan 4 at 22:02
add a comment |
When quoting error messages, please indicate which line of your code it applies to.
– AShelly
Jan 4 at 0:12
Your immediate exception is caused by the fact thattreedoens't return anything (which is the same as returningNonein Python). Sofor perm in tree(56)fails, astree(56)isNone, which is not iterable. This is probably not the only problem with your code, but I honestly don't know what you're trying to do, so I can't really help with anything else.
– Blckknght
Jan 4 at 0:55
Changing 'multitude' to 'complete range' probably isn't sufficient disambiguation to get you an answer -- perhaps a rough mock up of expected output would help clarify.
– cdlane
Jan 4 at 17:56
Welcome to SO! I'm voting to close as unclear what's being asked. Please post your expected output and clarify what you mean by "mass", "stem", "branch" and "layer".
– ggorlen
Jan 4 at 22:02
When quoting error messages, please indicate which line of your code it applies to.
– AShelly
Jan 4 at 0:12
When quoting error messages, please indicate which line of your code it applies to.
– AShelly
Jan 4 at 0:12
Your immediate exception is caused by the fact that
tree doens't return anything (which is the same as returning None in Python). So for perm in tree(56) fails, as tree(56) is None, which is not iterable. This is probably not the only problem with your code, but I honestly don't know what you're trying to do, so I can't really help with anything else.– Blckknght
Jan 4 at 0:55
Your immediate exception is caused by the fact that
tree doens't return anything (which is the same as returning None in Python). So for perm in tree(56) fails, as tree(56) is None, which is not iterable. This is probably not the only problem with your code, but I honestly don't know what you're trying to do, so I can't really help with anything else.– Blckknght
Jan 4 at 0:55
Changing 'multitude' to 'complete range' probably isn't sufficient disambiguation to get you an answer -- perhaps a rough mock up of expected output would help clarify.
– cdlane
Jan 4 at 17:56
Changing 'multitude' to 'complete range' probably isn't sufficient disambiguation to get you an answer -- perhaps a rough mock up of expected output would help clarify.
– cdlane
Jan 4 at 17:56
Welcome to SO! I'm voting to close as unclear what's being asked. Please post your expected output and clarify what you mean by "mass", "stem", "branch" and "layer".
– ggorlen
Jan 4 at 22:02
Welcome to SO! I'm voting to close as unclear what's being asked. Please post your expected output and clarify what you mean by "mass", "stem", "branch" and "layer".
– ggorlen
Jan 4 at 22:02
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031521%2fusing-recursion-to-build-various-trees%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031521%2fusing-recursion-to-build-various-trees%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When quoting error messages, please indicate which line of your code it applies to.
– AShelly
Jan 4 at 0:12
Your immediate exception is caused by the fact that
treedoens't return anything (which is the same as returningNonein Python). Sofor perm in tree(56)fails, astree(56)isNone, which is not iterable. This is probably not the only problem with your code, but I honestly don't know what you're trying to do, so I can't really help with anything else.– Blckknght
Jan 4 at 0:55
Changing 'multitude' to 'complete range' probably isn't sufficient disambiguation to get you an answer -- perhaps a rough mock up of expected output would help clarify.
– cdlane
Jan 4 at 17:56
Welcome to SO! I'm voting to close as unclear what's being asked. Please post your expected output and clarify what you mean by "mass", "stem", "branch" and "layer".
– ggorlen
Jan 4 at 22:02