[Answer]-Why doesn't this django mptt algorithm correctly compute rank? list index out

1👍

✅

You need to properly check if there are no children. In figure_rank, under.get_children() is returning an empty list, and then you try to access the first element. Check if the list is empty first.

def figure_rank(over, under):
    children = under.get_children()
    if chilren: # check if children is nonempty before accessing the first element
        r = children[0]
        over.count += 1
        figure_rank(over, r)
    else:
        return over.count

Leave a comment