There is a task for the pythontutor called "Pedigree: Ancestors and Descendants."
Condition:
Given two items in the tree. Determine whether one of them is a descendant of the other.
The input data contains a tree in the same format as in the previous task. Next comes the number of queries K. Each of the following K lines contains the names of two tree elements.
For each such query print one of three numbers: 1, if the first element is the ancestor of the second, 2, if the second is the ancestor of the first, or 0, if neither of them is the ancestor of the other.
To her given introductory (src_data.txt):
9 Alexei Peter_I Anna Peter_I Elizabeth Peter_I Peter_II Alexei Peter_III Anna Paul_I Peter_III Alexander_I Paul_I Nicholaus_I Paul_I 3 Anna Nicholaus_I Peter_II Peter_I Alexei Paul_I There is a code (well, that is, an attempt to solve):
def input(): return rows.pop(0) rows = [] file = open('src_data.txt', 'r') for line in file: rows.append(line[:-1]) # ======== Досюда это имитация ввода на PythonTutor === names = set() tree = [] for _ in range(int(input()), 1, -1): child, parrent = input().split() tree.append([child, parrent]) names.update([child, parrent]) for name in names: for item in tree: if name == item[0]: for i in range(len(tree)): if name == tree[i][-1]: tree[i].append(item[1]) # === Собственно проблема вот этом блоке (который выше) == # === Он то формирует списки как нужно, то недовставляет элементы == res_row = [] for _ in range(int(input())): pers_1, pers_2 = input().split() res = '0' for branch in tree: if pers_1 in branch and pers_2 in branch: if branch.index(pers_1) > branch.index(pers_2): res = '1' else: res = '2' res_row.append(res) print(' '.join(res_row)) And all would be nothing if it were not for random values in the output:
for i in {1..40}; do python3 first.py; sleep 1; done 0 2 0 0 2 0 0 2 0 1 2 0 0 2 0 1 2 0 1 2 0 0 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 0 2 0 Please explain how this can be and what is the error?