-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPython14_XML
More file actions
84 lines (70 loc) · 1.64 KB
/
Python14_XML
File metadata and controls
84 lines (70 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
## Python
## XML
## Find the Score
import sys
import xml.etree.ElementTree as etree
# Option 1
def get_attr_number(node):
score = 0
for child in node.iter():
score += len(child.attrib)
return score
# # Option 2
# def get_attr_number(node):
# score = 0
# score += len(node.attrib)
# for element in node:
# score += get_attr_number(element)
# return score
# # Option 3
# def get_attr_number(node):
# score = 0
# score += len(node.attrib)
# if len(node)>1:
# for i in node:
# score += get_attr_number(i)
# return score
if __name__ == '__main__':
sys.stdin.readline()
xml = sys.stdin.read()
tree = etree.ElementTree(etree.fromstring(xml))
root = tree.getroot()
print(get_attr_number(root))
## XML2 - Find the Maximum Depth
import xml.etree.ElementTree as etree
# # Option 1
# maxdepth = 0
# def depth(elem, level):
# global maxdepth
# level += 1
# maxdepth = max(maxdepth, level)
# for child in elem:
# depth(child, level)
# # Option 2
# maxdepth = 0
# arr = []
# def depth(elem, level):
# global maxdepth
# arr.append(level)
# for i in elem.getchildren():
# depth(i, level+1)
# maxdepth = max(arr)+1
# Option 3
maxdepth = 0
def depth(elem, level):
global maxdepth
i = 0
while i >= 0:
if elem.find("." + "/*" * i) is None:
maxdepth = i-1
break
i += 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)
## end ##