304 North Cardinal St.
Dorchester Center, MA 02124
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.
The first line contains N, the number of lines in the XML document.
The next N lines follow containing the XML document.
Output a single line, the integer value of the maximum level of nesting in the XML document.
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>
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.
# 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)
global maxdepth maxdepth = -1 def depth(elem, level): global maxdepth if (level == maxdepth): maxdepth += 1 for child in elem: depth(child, level + 1)
maxdepth = 0 def depth(elem, level): global maxdepth if (level == maxdepth): maxdepth += 1 for child in elem: depth(child, level + 1)
maxdepth = 0 def depth(elem, level): global maxdepth for child in elem: depth(child, level+1) maxdepth = max(maxdepth, level+2)
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
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.
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