All pastes #915050 Raw Edit

Harsh

public python v1 · immutable
#915050 ·published 2008-02-23 13:38 UTC
rendered paste body
#!/usr/bin/env pythonimport sysdef CreateGraph(graphlist):	# Split the node entry, i.e. ['1 0'] is split to ['1','0'].	graphlist = [each.split() for each in graphlist]	graph = {}	for each in graphlist:		# Add the nodes to the table Graph and;		# repeat again in a reverse fashion for;		# making it undirected.		if each[0] in graph:			graph[each[0]].append(each[1])		else:			graph[each[0]] = [each[1]]		if each[1] in graph:			graph[each[1]].append(each[0])		else:			graph[each[1]] = [each[0]]	return graphdef GetBestNode(graph):	# Make a list of the lengths of each node's connection;	# in order to find the node with the maximum no of;	# connections and return the final list after;	# filtering as required.	lenlist = [(len(graph[x]),x) for x in graph]	maximum = max(lenlist)[0]	return filter(lambda m: True if m[0] == maximum else False, lenlist)	if __name__ == '__main__':	# Read filename from CLI argument and open the file for reading.	graphlist = open(sys.argv[1]).readlines()	# Get the amount of testcases within the test file.	testcases = int(graphlist[0])	nodes=[]	i = 1	# Make seperate node index lists for each test case by reading;	# the no. of nodes per test case encountered and;	# incrementing along.	while i < len(graphlist):		x = int(graphlist[i])		nodes.append((i+1,i+x))		i+=int(graphlist[i])	graphs=[]	# For each test case of nodes, create a Graph by calling the;	# CreateGraph() function.	for start, end in nodes:		graphs.append(CreateGraph(graphlist[start:end]))	resultlist=[]	# Using the Graph tables created above, find the best nodes per;	# Graph and append to a resultant list for printing later;	# by calling GetBestNode() function.	for each in graphs:		resultlist.append([x[1] for x in GetBestNode(each)])	# Print each test case's results in a seperate line.	for each in resultlist:		for x in each:			print x,		print# Test input"""240 10 20 360 12 11 33 43 5"""