kbhusain
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
nodes = { 1: [ 2, 3, 4],
2: [ 1, 3 ],
3: [ 1, 2 ],
4: [ 1, ],
5: []}
def printGroups(n):
kids = nodes[n]; gps = [];
for k in kids:
gp = [k,]
for sk in nodes[k]:
if sk in kids and not sk in gp: # don't duplicate
gp.append(sk)
if len(gp) > 1:
gp.sort() # So you have an ordered list.
d = ",".join(["%d" % g for g in gp]) # if you have more than one.
else:
d = "%d" % k #A singleton
if not d in gps: gps.append(d)
for gp in gps: print gp # print per line
# ---------------------
if __name__ == '__main__':
printGroups(1)
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
- kbhusain April 26, 2016