XML 1 – Find the Score Hacker Rank Solution – Queslers

Problem : XML 1 – Find the Score Hacker Rank Solution

You are given a valid XML document, and you have to print its score. The score is calculated by the sum of the score of each element. For any element, the score is equal to the number of attributes it has.

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 score of the given 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 :

5

Explanation :

The feed and subtitle tag have one attribute each – lang.
The title and updated tags have no attributes.
The link tag has three attributes – rel, type and href.
So, the total score is 1+1+3 = 5.
There may be any level of nesting in the XML document. To learn about XML parsing, refer here.
NOTE: In order to parse and generate an XML element tree, use the following code:

>> import xml.etree.ElementTree as etree
>> tree = etree.ElementTree(etree.fromstring(xml))

Here, XML is the variable containing the string.
Also, to find the number of keys in a dictionary, use the len function:

>> dicti = {'0': 'This is zero', '1': 'This is one'}
>> print (len(dicti))

2

XML 1 – Find the Score Hacker Rank Solution in python 2

n = int(raw_input())
xml = ""
for i in range(n):
    xml += raw_input()
import xml.etree.ElementTree as etree
tree = etree.ElementTree(etree.fromstring(xml))
score = 0
for elem in tree.iter():
	score += len(elem.attrib)
print score

XML 1 – Find the Score Hacker Rank Solution in python 3

N = int(input())

def count_of_attr(root):
    count = len(root.attrib)
    for child in root:
        count += count_of_attr(child)
    return count

import xml.etree.ElementTree as etree
count = 0
xml = ''
for i in range(N):
    xml += input()

tree = etree.ElementTree(etree.fromstring(xml))
root = tree.getroot()
count = count_of_attr(root)
    
print(count)

XML 1 – Find the Score Hacker Rank Solution in pypy

def get_attr_number(node):
    # your code goes here
    count = len(node.attrib)
    for x in node:
        count += get_attr_number(x)
    return count

XML 1 – Find the Score Hacker Rank Solution in pypy 3

def get_attr_number(node):
    return len(node.attrib) + sum(get_attr_number(child) for child in node)
XML 1 – Find the Score Hacker Rank Solution Review:

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

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

XML 1 – Find the Score Hacker Rank Solution Conclusion:

I hope this XML 1 – Find the Score 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.