-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-8.56.py
85 lines (73 loc) · 1.4 KB
/
c-8.56.py
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
85
"""
The indented parenthetic representation of a tree T is a variation
of the parenthetic representation of T that uses indentation and line breaks. Give an
algorithm that prints this representation of a tree.
"""
def parenthetic_tree(el, d=0, step=2):
label = d * step * " " + el.tag
print(label, end='')
is_first_child = False
for c in el.iterchildren():
idx = el.index(c)
l = len(el)
if idx == 0: # first child
is_first_child = True
print(' (')
elif idx != l:
print(end='\n')
parenthetic_tree(c, d+1, step=step)
if is_first_child:
closing_tag = "\n" + d * step * " " + ')'
print(closing_tag, end='')
if __name__ == "__main__":
from lxml import etree as et
tree = et.parse('./input/trees/bookstore.xml')
root = tree.getroot()
parenthetic_tree(root, step=4)
"""
bookstore.xml, step=4
bookstore (
book (
title
author
year
price
sign (
test
fest
)
ign (
test
fest
)
)
book (
title
author
year
price
)
book (
title
author
year
price
)
)
"""
"""
bin_tree_1.xml, step=2 (default)
root (
one (
one_1
one_2 (
one_2_1
one_2_2
)
)
two (
two_1
two_2
)
)
"""