XML2 - Find the Maximum Depth

Sort by

recency

|

122 Discussions

|

  • + 0 comments
    import xml.etree.ElementTree as etree
    maxdepth = 0
    def depth(elem, level):
        global maxdepth
        maxdepth = max(maxdepth, level+1)
        for x in elem:
            depth(x, level+1)
    if __name__ == '__main__':
        n = int(input())
        xml = ""
        for i in range(n):
            xml =  xml + input() + "\n"
        tree = etree.ElementTree(etree.fromstring(xml))
        depth(tree.getroot(), -1)
        print(maxdepth)
    
  • + 0 comments

    Finally iv found a one liner. Im gona tell my doc im ready to quit Xanax .

    maxdepth = 0
    def depth(elem, level= 0):
        global maxdepth
        maxdepth = max(maxdepth, level + 1) if [depth(e, level + 1) for e in elem] else max(maxdepth, level + 1)
    
  • + 0 comments

    Im so upset i couldnt find any oneliner able to work ..

    def depth(elem, level= 0):
        global maxdepth
        [depth(e,level+1) for e in elem]
        maxdepth = max(maxdepth, level + 1)
    
  • + 0 comments

    Working around the constraints of the call...

    maxdepth = 0
    
    def _depth(elem, level=0):
        if len(elem) == 0:
            return level
        return max(_depth(child, level + 1) for child in elem)
        
    def depth(elem, level=0):
        global maxdepth
        maxdepth = _depth(elem, 0)
    
  • + 0 comments
    maxdepth = 0
    def depth(elem, level):
        global maxdepth
        level = level + 1
        for e in elem:
            depth(e, level)
        if(level > maxdepth):
            maxdepth = level``