Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. raw = """animal
  2. carnivorous
  3. tiger
  4. lion
  5. vegetarian
  6. cow
  7. sheep
  8. plant
  9. algea
  10. tree
  11. leaf
  12. pine
  13. fungus
  14. good
  15. bad
  16. evil
  17. mean
  18. cactus
  19. big
  20. small"""
  21.  
  22. lines = raw.split('n')
  23. indents = [(0,0,'root')]
  24. for a in raw.split('n'):
  25. indent = 0
  26. while a[indent] == ' ': indent+=1
  27. if indent % 4:
  28. print("not multiple of 4")
  29. break
  30. indents.append((len(indents), int(indent/4)+1,a.replace(' ','')))
  31. for a in indents: print(a)
  32.  
  33. stack=[indents[0]]
  34. entries =[indents[0]]
  35.  
  36. prev_indent = 0
  37. for item in indents[1:]:
  38. print("#########################")
  39. id, indent, name = item
  40. diff = indent - prev_indent
  41. print(item)
  42. print("diff",diff, [a[2] for a in stack])
  43.  
  44. if diff>0:
  45. entries.append(item+(stack[-1][2],))
  46. elif diff<0:
  47. # entries.append(item+(stack[-diff][2],))
  48. count = -diff
  49. while count>-1: stack.pop();count-=1
  50. entries.append(item+(stack[-1][2],))
  51. elif diff==0:
  52. stack.pop()
  53. entries.append(item+(stack[-1][2],))
  54. stack.append(item)
  55. prev_indent = entries[-1][1]
  56.  
  57. print("result", entries[-1])
  58. print("########################")
  59.  
  60.  
  61. for a in entries:
  62. if len (a) == 3: continue
  63. ident, level, name, parent = a
  64. print(level*' '*4, name, '(', parent, ')')
  65.  
  66. animal ( root )
  67. carnivorous ( animal )
  68. tiger ( carnivorous )
  69. lion ( carnivorous )
  70. vegetarian ( animal )
  71. cow ( vegetarian )
  72. sheep ( vegetarian )
  73. plant ( root )
  74. algea ( plant )
  75. tree ( plant )
  76. leaf ( tree )
  77. pine ( tree )
  78. fungus ( plant )
  79. good ( fungus )
  80. bad ( fungus )
  81. evil ( bad )
  82. mean ( bad )
  83. cactus ( plant )
  84. big ( cactus )
  85. small ( cactus )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement