Detect HTML Attributes

  • + 0 comments

    Testcase fails.

    Copy testcase STDIN to custom input case box.

    TEST SUCCESSFUL.

    WHAT GIVES!!!

    Not the prettiest code, but it definitely worked:

    import re
    
    n = int(input())
    
    tag_dict = dict()
    RE = re.compile(r'<\w+|\w+=')
    for _ in range(n):
        html = RE.findall(input())
        for i, tag in enumerate(html):
            if '<' in tag:
                key = tag[1:]
                tag_dict[key] = []
                for attr in html[i:]:
                    if '<' in attr and attr is not tag:
                        break
                    elif '=' in attr:
                        tag_dict[key].append(attr)
      
    for key in sorted(tag_dict.keys()):
            print(f'{key}:{",".join(sorted([attr for attr in tag_dict[key]]))}')