#🔒 Python Recursion Help

36 messages · Page 1 of 1 (latest)

runic pine
#

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.

scenic kayakBOT
#

@runic pine

Python help channel opened

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.

runic pine
#

!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))
scenic kayakBOT
#

@runic pine :white_check_mark: Your 3.12 eval job has completed with return code 0.

0
pliant current
#

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:
    ...
pliant current
#

this is an issue

runic pine
#

replied to the wrong message oops

pliant current
#

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

pliant current
#

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)
)

||
runic pine
pliant current
#

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

runic pine
pliant current
#

you can also sometimes make tuples without the ()

#

!e

x = 0, 1
print(x)
scenic kayakBOT
#

@pliant current :white_check_mark: Your 3.12 eval job has completed with return code 0.

(0, 1)
runic pine
#

ahh okay i see💯

runic pine
pliant current
#
1 + 2 * 3 == 7
(1 + 2) * 3 == 9
runic pine
#

ohhhhhhhhhh i didnt know they could be used like that

pliant current
#

it's just like math

#

oh wait I just realized you meant when they are extraneous order-wise, but allow indentation and newlining

pliant current
#

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

scenic kayakBOT
#
Python help channel closed

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.