Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s = input()
- # s = '(asciitree (sometimes (you) just (want (to draw)) trees in (your (terminal))))'
- # build a tree
- tree = []
- indent = 0
- max_indent = 0
- is_first = True
- for l in s.split('('):
- for r in l.split(')'):
- for m in r.split(' '):
- w = m.strip()
- if w != '':
- if is_first: # consider first element as a root element
- indent = 0
- is_first = False
- if indent > max_indent: max_indent = indent
- if indent > 0: tree.append(' '*(indent-1)+'+-- '+w)
- else: # do not draw a branch for root elements
- tree.append(w)
- indent = 0 # in case first element wasn't a top one
- indent -= 1
- indent += 2
- # add vertical lines between two '+'
- def draw_vertical_line(ind, a, b):
- for i in range(a+1, b):
- tree[i] = tree[i][:ind*4]+'|'+tree[i][ind*4+1:]
- return
- # search for two vertically aligned '+'
- for i in range(max_indent):
- a_found = False
- for j in range(len(tree)):
- if len(tree[j]) > i*4:
- if tree[j][i*4] == '+':
- if a_found: draw_vertical_line(i, a, j)
- else: a_found = True
- a = j
- elif tree[j][i*4] != ' ': a_found = False
- else: a_found = False
- # print out the tree
- for line in tree: print(line)
Add Comment
Please, Sign In to add comment