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.
I don't know why, but I was pretty sure that we have to do recursive method to solve this problem. I mean, I did that, and it works. But when I see others solutions, I have seen that node.iter() just go through every line of the xml document which is, I think, better.
Anyway, here my code :
importsysimportxml.etree.ElementTreeasetreedefget_attr_number(node):# your code goes herecount=len(node.attrib)forchildinnode:count+=get_attr_number(child)returncountif__name__=='__main__':sys.stdin.readline()xml=sys.stdin.read()tree=etree.ElementTree(etree.fromstring(xml))root=tree.getroot()print(get_attr_number(root))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
XML 1 - Find the Score
You are viewing a single comment's thread. Return to all comments →
I don't know why, but I was pretty sure that we have to do recursive method to solve this problem. I mean, I did that, and it works. But when I see others solutions, I have seen that node.iter() just go through every line of the xml document which is, I think, better.
Anyway, here my code :