Header Ad

HackerRank XML2 Find the Maximum Depth solution in python

In this XML2 find the maximum depth problem, 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 0.

HackerRank XML2 Find the Maximum Depth solution in python


Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
def depth(tree):
    dp = 0
    if tree is not None:
        for i in tree:
            dp = max(dp,  depth(i) + 1)
    return dp
n = int(raw_input())
xml = ""
for i in range(n):
    xml += raw_input()
import xml.etree.ElementTree as etree
tree = etree.fromstring(xml)


print depth(tree)



Problem solution in Python 3 programming.

global maxdepth
maxdepth = -1
def depth(elem, level):
    global maxdepth
    if (level == maxdepth):
        maxdepth += 1
        
    for child in elem:
        depth(child, level + 1)


Problem solution in pypy programming.

maxdepth = 0
def depth(elem, level):
    global maxdepth
    
    if (level == maxdepth):
        maxdepth += 1
        
    for child in elem:
        depth(child, level + 1)


Problem solution in pypy3 programming.

maxdepth = 0
def depth(elem, level):
    global maxdepth

    for child in elem:
        depth(child, level+1)
        maxdepth = max(maxdepth, level+2)


Post a Comment

0 Comments