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.
As a huge proponent of the DRY principle (Don't Repeat Yourself), the below is a way of running this script:
# Enter your code here. Read input from STDIN. Print output to STDOUTfromhtml.parserimportHTMLParserdefprint_tagattrs(tag,attrs):print(tag)[print(f'->{attr}>{val}')for(attr,val)inattrs]classMyHTMLParser(HTMLParser):defhandle_starttag(self,tag,attrs):print_tagattrs(tag,attrs)defhandle_startendtag(self,tag,attrs):print_tagattrs(tag,attrs)parser=MyHTMLParser()parser.feed("".join([input()for_inrange(int(input()))]))
You can make the print_tagattrs methods part of the class, or not, up to you, but I think this is a fairly straightforward DRY implmentation. If you really wanted, you could one-line the last two lines to get:
Detect HTML Tags, Attributes and Attribute Values
You are viewing a single comment's thread. Return to all comments →
As a huge proponent of the DRY principle (Don't Repeat Yourself), the below is a way of running this script:
You can make the print_tagattrs methods part of the class, or not, up to you, but I think this is a fairly straightforward DRY implmentation. If you really wanted, you could one-line the last two lines to get:
Note: this reduces readibility.