Hi, i have my code below that I will run with the bot. My problem is the output of the code. this function im writing is supposed to add to the counter every time it comes across a number lower then seven, which should be twice since 5 and 6 are both less then 7. however, im getting 0. Im sure its a recursion problem that I cant think of.
#🔒 Python Recursion Help
36 messages · Page 1 of 1 (latest)
@runic pine
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
!e
class treenode(object):
def __init__(self, data, left=None, right=None):
"""
Create a new treenode for the given data.
Pre-conditions:
data: Any data value to be stored in the treenode
left: Another treenode (or None, by default)
right: Another treenode (or None, by default)
"""
self.data = data
self.left = left
self.right = right
def count_smaller(tnode, target):
"""
Purpose: To count the number of data values in the given treenode that are smaller than the given target value.
:param tnode: a given treenode
:param target: a given target value (assumed to be numerical)
:return: integer number of data values that are smaller than the given target value
"""
counter=0
if not tnode:
return counter
if tnode.left is None and tnode.right is None:
return counter
else:
if target > tnode.data:
counter+=1
if tnode.left:
counter+=count_smaller(tnode.left,target)
if tnode.right:
counter+=count_smaller(tnode.right,target)
return counter
tnode1=treenode(5,None,None)
tnode2=treenode(6,None,None)
tnode3=treenode(7,tnode1,tnode2)
print(count_smaller(tnode3,7))
@runic pine :white_check_mark: Your 3.12 eval job has completed with return code 0.
0
I'll look into this gimme a few minutes. Side note: classes and types should be named per PascalCase.
||and there are some spacing issues such as missing space after command, and missing spaces around assignment operator||
oh also you don't need to explicitly inherit from object, because all classes already inherit from object
class TreeNode:
...
if tnode.left is None and tnode.right is None:
return 0 # was `return counter`, but counter always equals 0 here
this is an issue
ohhhh so that should have been return 0 afterall?
replied to the wrong message oops
I mean, it's the same thing I just rewrote it to be clearer on why there is an issue
count_smaller(TreeNode(0), 1) == 0
with your current code
really this whole if statement shouldn't be there
the first if handles the case that follows it
1. if tnode is None, then return 0
2. if target is greater than tnode's data, add 1 (can be added to next step)
3. unconditionally add recursive call for each left and right
4. return counter
really this only needs one if statement
spoilers for how I would solve it
||```py
def count_smaller(node, target):
if node is None:
return 0
return (
(node.data < target)
+ count_smaller(node.left, target)
+ count_smaller(node.right, target)
)
||
i see💯 whats this method of putting that return statement in brackets called? are those tuples?
no that is just an order-of-operations parenthesis, which normally would be extraneous here, but it's allowing to have newlines in the expression. Without it I would have to write all those parts on one line
ahh yes it seems like i wrote an unneccesary base case
a tuple is (a, b, c), which is comma-separated and surrounded by ()
you can also sometimes make tuples without the ()
!e
x = 0, 1
print(x)
@pliant current :white_check_mark: Your 3.12 eval job has completed with return code 0.
(0, 1)
ahh okay i see💯
this is the first time ive seen the order of operations parenthesis
that is definitely wrong
1 + 2 * 3 == 7
(1 + 2) * 3 == 9
ohhhhhhhhhh i didnt know they could be used like that
it's just like math
oh wait I just realized you meant when they are extraneous order-wise, but allow indentation and newlining
it's because any code inside (), [], {} ignores newlines and indentation
so adding () around doesn't change the order of operations, but it does allow me to reformat the expression in a way I find more readable
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.