Thursday, December 12, 2019

XML2 - Find the Maximum Depth - Hacker Rank Solution

You are given a valid XML document, and you have to print the maximum level of nesting in it. Take the depth of the root as .

XML2 - Find the Maximum Depth - Hacker Rank Solution

import xml.etree.ElementTree as etree

maxdepth = 0
def depth(elem, level):
    global maxdepth
    level += 1
    if (maxdepth < level):
        maxdepth = level
    for child in elem:
        depth(child, level)
        
if __name__ == '__main__':
    n = int(raw_input())
    xml = ""
    for i in range(n):
        xml =  xml + raw_input() + "\n"
    tree = etree.ElementTree(etree.fromstring(xml))
    depth(tree.getroot(), -1)
    print maxdepth

No comments:

Post a Comment

Powered by Blogger.