Skip to content

Instantly share code, notes, and snippets.

@avagin
Created May 10, 2017 19:59
Show Gist options
  • Save avagin/e6e3bcefc252d38ab3065442b627a71d to your computer and use it in GitHub Desktop.
Save avagin/e6e3bcefc252d38ab3065442b627a71d to your computer and use it in GitHub Desktop.
[avagin@laptop ~]$ cat t.py
import sys
items = {}
class Item:
def __init__(self, key, parent):
self.parent = parent
self.key = key
self.children = []
pass
f = open(sys.argv[1])
k = f.readline().split()[0]
root = item = Item(k, None)
items[k] = item
for l in f:
k, p = l.split()
item = Item(k, items[p])
items[k] = item
items[p].children.append(item)
next_level = [root]
r = 0;
while len(next_level):
level = next_level
next_level = []
r = r + 1
level.reverse()
for i in level:
next_level.extend(i.children)
print i.key,
print
[avagin@laptop ~]$ cat input
F
B F
G F
A B
D B
I G
C A
E A
H I
[avagin@laptop ~]$ python t.py input
F
G B
D A I
H E C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment