HackerRank Python - XML 1 - Find the Score

You are given a valid XML document, and you have to print its score. The score is calculated by the sum of the score of each element. For any element, the score is equal to the number of attributes it has.

  1. import sys
  2. import xml.etree.ElementTree as etree
  3. def get_attr_number(node):
  4. return len(node.attrib) + sum(get_attr_number(child) for child in node);
  5. if __name__ == '__main__':
  6. sys.stdin.readline()
  7. xml = sys.stdin.read()
  8. tree = etree.ElementTree(etree.fromstring(xml))
  9. root = tree.getroot()
  10. print(get_attr_number(root))

download android app