Жангирхан Шаку, 404 MroC3 12991
Арсен Жуматай,304 MroC3 12993
n1def parse_class_definitions(code_block):n1def extract_classes(code):
2    classes_info = {}2    result = {}
3    for line in code_block.splitlines():3    for statement in code.splitlines():
4        line = line.strip()4        statement = statement.strip()
5        if not line.startswith('class '):5        if not statement.startswith('class '):
6            continue6            continue
n7        name_end_pos = line.find('(')n7        class_name_end = statement.find('(')
8        if name_end_pos == -1:8        if class_name_end == -1:
9            name_end_pos = line.find(':')9            class_name_end = statement.find(':')
10        class_name = line[6:name_end_pos].strip()10        class_name = statement[6:class_name_end].strip()
11        if '(' in line:11        if '(' in statement:
12            parents_start_pos = line.find('(') + 112            parents_start = statement.find('(') + 1
13            parents_end_pos = line.find(')')13            parents_end = statement.find(')')
14            parents = line[parents_start_pos:parents_end_pos].strip()14            parents = statement[parents_start:parents_end].strip()
15            parent_classes = [parent.strip() for parent in parents.split15            parents_list = [parent.strip() for parent in parents.split('
>(',')] if parents else []>,')] if parents else []
16        else:16        else:
n17            parent_classes = []n17            parents_list = []
18        classes_info[class_name] = parent_classes18        result[class_name] = parents_list
19    return classes_info19    return result
2020
n21def check_class_hierarchy(classes_info):n21def validate_hierarchy(classes):
2222
n23    def compute_mro(class_name, resolved_hierarchy, unresolved_classes):n23    def resolve_mro(class_name, resolved, unresolved):
24        if class_name in resolved_hierarchy:24        if class_name in resolved:
25            return resolved_hierarchy[class_name]25            return resolved[class_name]
26        if class_name in unresolved_classes:26        if class_name in unresolved:
27            raise ValueError('Circular inheritance detected')27            raise ValueError('Cyclic dependency detected')
28        unresolved_classes.add(class_name)28        unresolved.add(class_name)
29        parent_classes = classes_info.get(class_name, [])29        bases = classes.get(class_name, [])
30        mro = [class_name] + merge_mros([compute_mro(parent, resolved_hi30        mro = [class_name] + combine_mros([resolve_mro(base, resolved, u
>erarchy, unresolved_classes) for parent in parent_classes] + [parent_cla>nresolved) for base in bases] + [bases])
>sses]) 
31        resolved_hierarchy[class_name] = mro31        resolved[class_name] = mro
32        unresolved_classes.remove(class_name)32        unresolved.remove(class_name)
33        return mro33        return mro
3434
n35    def merge_mros(mro_sequences):n35    def combine_mros(sequences):
36        merged_result = []36        result = []
37        while any(mro_sequences):37        while any(sequences):
38            for seq in mro_sequences:38            for seq in sequences:
39                if not seq:39                if not seq:
40                    continue40                    continue
n41                candidate_class = seq[0]n41                candidate = seq[0]
42                if all((candidate_class not in s[1:] for s in mro_sequen42                if all((candidate not in s[1:] for s in sequences)):
>ces)): 
43                    merged_result.append(candidate_class)43                    result.append(candidate)
44                    for s in mro_sequences:44                    for s in sequences:
45                        if s and s[0] == candidate_class:45                        if s and s[0] == candidate:
46                            s.pop(0)46                            s.pop(0)
47                    break47                    break
48            else:48            else:
n49                raise ValueError('Invalid MRO structure')n49                raise ValueError('Invalid MRO')
50        return merged_result50        return result
51    resolved_hierarchy = {}51    resolved = {}
52    for class_name in classes_info:52    for class_name in classes:
53        compute_mro(class_name, resolved_hierarchy, set())53        resolve_mro(class_name, resolved, set())
54    return True54    return True
n55input_program = ''n55input_code = ''
56while (user_input := input().strip()):56while (line := input().strip()):
57    input_program += user_input + '\n'57    input_code += line + '\n'
58try:58try:
t59    class_data = parse_class_definitions(input_program)t59    class_structure = extract_classes(input_code)
60    print('Yes' if check_class_hierarchy(class_data) else 'No')60    print('Yes' if validate_hierarchy(class_structure) else 'No')
61except ValueError:61except ValueError:
62    print('No')62    print('No')
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op