Guest User

Untitled

a guest
Feb 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. s = input()
  2. # s = '(asciitree (sometimes (you) just (want (to draw)) trees in (your (terminal))))'
  3.  
  4. # build a tree
  5. tree = []
  6. indent = 0
  7. max_indent = 0
  8. is_first = True
  9.  
  10. for l in s.split('('):
  11. for r in l.split(')'):
  12. for m in r.split(' '):
  13. w = m.strip()
  14. if w != '':
  15. if is_first: # consider first element as a root element
  16. indent = 0
  17. is_first = False
  18.  
  19. if indent > max_indent: max_indent = indent
  20.  
  21. if indent > 0: tree.append(' '*(indent-1)+'+-- '+w)
  22. else: # do not draw a branch for root elements
  23. tree.append(w)
  24. indent = 0 # in case first element wasn't a top one
  25. indent -= 1
  26. indent += 2
  27.  
  28.  
  29. # add vertical lines between two '+'
  30. def draw_vertical_line(ind, a, b):
  31. for i in range(a+1, b):
  32. tree[i] = tree[i][:ind*4]+'|'+tree[i][ind*4+1:]
  33. return
  34.  
  35.  
  36. # search for two vertically aligned '+'
  37. for i in range(max_indent):
  38. a_found = False
  39. for j in range(len(tree)):
  40. if len(tree[j]) > i*4:
  41. if tree[j][i*4] == '+':
  42. if a_found: draw_vertical_line(i, a, j)
  43. else: a_found = True
  44. a = j
  45. elif tree[j][i*4] != ' ': a_found = False
  46. else: a_found = False
  47.  
  48. # print out the tree
  49. for line in tree: print(line)
Add Comment
Please, Sign In to add comment