We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
for tag in sorted(tags):
if tag == 'a':
attrs = ','.join(sorted([attr for attr in tags[tag] if attr in ['accesskey', 'href', 'title']]))
else:
attrs = ','.join(sorted(tags[tag]))
if attrs:
print("%s:%s" % (tag, attrs))
else:
print("%s:" % tag)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Detect HTML Attributes
You are viewing a single comment's thread. Return to all comments →
The exact code will be: import re
Read input
N = int(raw_input()) html = '' for _ in range(N): html += raw_input()
Extract tags and attributes using regex
tags = {} pattern = r'<(\w+)([^>]*)>' matches = re.findall(pattern, html)
for match in matches: tag, attrs = match if tag not in tags: tags[tag] = set()
Print output
for tag in sorted(tags): if tag == 'a': attrs = ','.join(sorted([attr for attr in tags[tag] if attr in ['accesskey', 'href', 'title']])) else: attrs = ','.join(sorted(tags[tag]))