• + 0 comments

    You can do it without importing modules but yeah it's a little longer

    n = int(input())
    char = ['1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F']
    not_selector, string = False, ""
    for _ in range(n):
        line = input()
        if "}" in line:
            string += line[:line.index("}")]
            not_selector = False
        elif "{" in line:
            string += line[line.index("{"):]
            not_selector = True
        elif not_selector:
            string += line
    hex_ind = [i for i in range(len(string)) if string[i] == "#"] 
    for i in hex_ind:
        hex_code = "0"
        while hex_code.isalnum():
            hex_code += string[i+1]
            i += 1
        is_valid = True
        for i in hex_code[1:-1]:
            is_valid = is_valid and (i.upper() in char)
        if is_valid:
            print("#" + hex_code[1:-1])