XML 2 – Find the Maximum Depth Hacker Rank Solution – Queslers

Problem : XML 2 – 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 0.

Input Format :

The first line contains N, the number of lines in the XML document.
The next N lines follow containing the XML document. 

Output Format :

Output a single line, the integer value of the maximum level of nesting in the XML document.

Sample Input :

6
<feed xml:lang='en'>
    <title>HackerRank</title>
    <subtitle lang='en'>Programming challenges</subtitle>
    <link rel='alternate' type='text/html' href='http://hackerrank.com/'/>
    <updated>2013-12-25T12:00:00</updated>
</feed>

Sample Output :

1

Explanation :

Here, the root is a feed tag, which has depth of 0.
The tags title, subtitle, link and updated all have a depth of 1.
Thus, the maximum depth is 1. 

XML 2 – Find the Maximum Depth Hacker Rank Solution in python 2

# 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)

XML 2 – Find the Maximum Depth Hacker Rank Solution in python 3

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

XML 2 – Find the Maximum Depth Hacker Rank Solution in pypy

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

XML 2 – Find the Maximum Depth Hacker Rank Solution in pypy 3

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

    for child in elem:
        depth(child, level+1)
        maxdepth = max(maxdepth, level+2)
XML 2 – Find the Maximum Depth Hacker Rank Solution Review:

In our experience, we suggest you solve this XML 2 – Find the Maximum Depth Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

XML 2 – Find the Maximum Depth is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

XML 2 – Find the Maximum Depth Hacker Rank Solution Conclusion:

I hope this XML 2 – Find the Maximum Depth Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published.