view req_dep.py @ 45:7d4da4e72d37

fix argv handling
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 05 Jul 2022 10:22:50 +0100
parents e07789816ca5
children
line wrap: on
line source

#!/usr/bin/python2.7
import sys
kids={}
parents={}
known=set(())
l=sys.stdin.readline()
while l:
  ff=l.strip().split()
  if len(ff)!=0:
    d=ff[0]
    sys.stdin.readline()
    pp=sys.stdin.readline().strip().replace(',','').split()[2:]
    parents[d]=pp
    known.add(d)
    for p in pp:
      known.add(p)
      try:
        c=kids[p]
        c.add(d)
      except KeyError:
        c=set([d])
        kids[p]=c
  l=sys.stdin.readline()
roots={}
still=set(())
for k in known:
  if k in parents:
    still.add(k)
  else:
    roots[k]=set([k])
#print len(parents),len(kids),len(roots),len(still),roots
print roots.keys()
while len(still)>0:
  #print len(still),len(roots)
  pend=still
  still=set([])
  while len(pend)>0:
    x=pend.pop()
    #print x,len(pend)
    for p in parents[x]:
      try:
        r=roots[p]
        roots[x]=roots.get(x,set(())).union(r)
      except KeyError:
        if p in parents:
          still.add(x)
for x,r in sorted(roots.items()):
  print x,r