Mercurial > hg > python
comparison req_dep.py @ 2:e07789816ca5
adding more python files from lib/python on origen
author | Henry Thompson <ht@markup.co.uk> |
---|---|
date | Mon, 09 Mar 2020 16:48:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:0a3abe59e364 | 2:e07789816ca5 |
---|---|
1 #!/usr/bin/python2.7 | |
2 import sys | |
3 kids={} | |
4 parents={} | |
5 known=set(()) | |
6 l=sys.stdin.readline() | |
7 while l: | |
8 ff=l.strip().split() | |
9 if len(ff)!=0: | |
10 d=ff[0] | |
11 sys.stdin.readline() | |
12 pp=sys.stdin.readline().strip().replace(',','').split()[2:] | |
13 parents[d]=pp | |
14 known.add(d) | |
15 for p in pp: | |
16 known.add(p) | |
17 try: | |
18 c=kids[p] | |
19 c.add(d) | |
20 except KeyError: | |
21 c=set([d]) | |
22 kids[p]=c | |
23 l=sys.stdin.readline() | |
24 roots={} | |
25 still=set(()) | |
26 for k in known: | |
27 if k in parents: | |
28 still.add(k) | |
29 else: | |
30 roots[k]=set([k]) | |
31 #print len(parents),len(kids),len(roots),len(still),roots | |
32 print roots.keys() | |
33 while len(still)>0: | |
34 #print len(still),len(roots) | |
35 pend=still | |
36 still=set([]) | |
37 while len(pend)>0: | |
38 x=pend.pop() | |
39 #print x,len(pend) | |
40 for p in parents[x]: | |
41 try: | |
42 r=roots[p] | |
43 roots[x]=roots.get(x,set(())).union(r) | |
44 except KeyError: | |
45 if p in parents: | |
46 still.add(x) | |
47 for x,r in sorted(roots.items()): | |
48 print x,r |