XML2 - Find the Maximum Depth
Create Date: March 31, 2019 at 02:59 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
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 .
Input Format
The first line contains , the number of lines in the XML document.
The next 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 .
The tags title, subtitle, link and updated all have a depth of .
Thus, the maximum depth is .
import xml.etree.ElementTree as etree
maxdepth = 0
def depth(elem, level):
global maxdepth
# your code goes here
if (level == maxdepth):
maxdepth += 1
for child in elem:
depth(child, 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)
New Comment
XML 1 - Find the Score
Create Date: March 31, 2019 at 02:24 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
ou 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 , the number of lines in the XML document.
The next 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 .
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
import sys
import xml.etree.ElementTree as etree
def get_attr_number(node):
# your code goes here
#return sum([len(elem.items()) for elem in node.iter()])
#return etree.tostring(node).count(b'=')
return sum(map(get_attr_number, node)) + len(node.attrib)
if __name__ == '__main__':
sys.stdin.readline()
xml = sys.stdin.read()
tree = etree.ElementTree(etree.fromstring(xml))
root = tree.getroot()
print(get_attr_number(root))
New Comment
Class 2 - Find the Torsional Angle
Create Date: March 31, 2019 at 12:31 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
You are given four points and in a 3-dimensional Cartesian coordinate system. You are required to print the angle between the plane made by the points and in degrees(not radians). Let the angle be .
where x and x .
Here, means the dot product of and , and x means the cross product of vectors and . Also, .
Input Format
One line of input containing the space separated floating number values of the and coordinates of a point.
Output Format
Output the angle correct up to two decimal places.
Sample Input
0 4 5
1 7 6
0 5 9
1 7 2
Sample Output
8.19
import math
class Points(object):
def __init__(self, x, y, z):
self.x = float(x)
self.y = float(y)
self.z = float(z)
def __add__(self, no):
return Points(self.x + no.x, self.y + no.y, self.z + no.z)
def __sub__(self, no):
return Points(self.x - no.x, self.y - no.y, self.z - no.z)
def dot(self, no):
return self.x * no.x + self.y * no.y + self.z * no.z
def cross(self, no):
return Points(self.y * no.z - self.z * no.y, self.z * no.x - self.x * no.z, self.x * no.y - self.y * no.x)
def absolute(self):
return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)
if __name__ == '__main__':
points = list()
for i in range(4):
a = list(map(float, input().split()))
points.append(a)
a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3])
x = (b - a).cross(c - b)
y = (c - b).cross(d - c)
angle = math.acos(x.dot(y) / (x.absolute() * y.absolute()))
print("%.2f" % math.degrees(angle))
New Comment