Created
May 10, 2017 19:59
-
-
Save avagin/e6e3bcefc252d38ab3065442b627a71d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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, | |
[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