-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_tree.py
More file actions
40 lines (33 loc) · 1.03 KB
/
display_tree.py
File metadata and controls
40 lines (33 loc) · 1.03 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
# How to use: python display_tree.py atb3v31.sample
# the output is printed on the terminal
import sys
if __name__ == "__main__":
try:
file_name = sys.argv[1] # Get GOLD file name from user input
except IndexError:
print ('Missing argument')
raise SystemExit
try:
inputfile = open(file_name, 'r')
except IOError:
print ('File(s) not found')
raise SystemExit
tree = inputfile.readline()
while tree:
spaces = 0
stack = []
for index in range(len(tree)):
sys.stdout.write(tree[index])
if tree[index] =='(': stack.append(spaces)
if tree[index] ==')':
# stack.pop()
if tree[index+1] !=')':
sys.stdout.write('\n'+' '*(stack[-1]-1))
spaces = stack[-1]-2
stack.pop()
else:
stack.pop()
spaces = spaces+1
tree = inputfile.readline()
print '\n'
inputfile.close()