S
S
slavatarasov2021-06-22 22:38:15
Python
slavatarasov, 2021-06-22 22:38:15

Why does RunTime error occur when calling recursion?

Greetings, I'm tormenting the task for the second day and I don't know what's wrong with it.
1 - 3 screen condition of the problem
4 - incorrect answers
60d23a663799e351209326.jpeg60d23a7160b60456507325.jpeg60d23a7abafc3341567595.jpeg60d23a9581e46534750718.jpeg

Gives an error RunTime error I will assume that it is a matter of recursion, but at the same time, according to the condition of the problem, a closed loop should not occur (I solve it using graphs)

all_classes = {}
for _ in range(int(input())):
    request = input().split()
    if len(request) == 1:
        if request[0] not in all_classes:
            all_classes[request[0]] = []
        else:
            all_classes[request[0]] += request[0]
    else:
        del request[1]
        if request[0] not in all_classes:
            all_classes[request[0]] = []
            for parent in request[1:]:
                all_classes[request[0]] += parent
        else:
            for parent in request[1:]:
                all_classes[request[0]] += parent
def parent_width_search(child):
    global count
    if request[0] in all_classes[child]:
        count += 1
        return
    else:
        for parent in all_classes[child]:
            parent_width_search(parent)
            if count >= 1:
                return
for _ in range(int(input())):
    count = 0
    request = input().split()
    if request[0] == request[1]:
        print('Yes')
    elif request[1] not in all_classes:
        print('No')
    else:
        parent_width_search(request[1])
        print('Yes' if count >= 1 else 'No')


the problem is in the parent_width_search function, but I can’t understand what exactly, since all the usual tests pass normally

; the inheritance system is implemented through a dictionary where:
{child class: [elements of parents]}
construction:
{introduced class: []}

The ancestor search system goes like this:
1) checks if the child class has the desired ancestor, if it does, then increases count by 1, this is necessary so that later in the print () program it displays "Yes " if count has increased by at least 1 (that is, it has found at least one ancestor of the child class)
2) If not, then it takes all the ancestors of this class and runs each one through recursion
3) if found again, then increases count by 1


checked on such a test, there were no problems, it also says in the condition that the class cannot be inherited from itself, explicitly or implicitly, so I can’t understand how my recursion could loop at all 60d23a451281a781869575.jpeg, there were no problems, it also says in the condition that the class cannot inherit from itself explicitly or implicitly, so it is unlikely that this causes a recursion loop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
slavatarasov, 2021-06-22
@slavatarasov

The question is closed. It turns out that when reading the request, I did not take into account that when adding a string to the list using +=, the string is added letter by letter, and not completely

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question