I need a python script of this question, using the set() function and please as
ID: 3592010 • Letter: I
Question
I need a python script of this question, using the set() function and please as simple as possible. Thanks for your time! In your class, many students are friends. Let's assume that two students sharing a friend must be friends themselves; in other words, if students 0 and 1 are friends and students l and 2 are friends, then students 0 and 2 must be friends. Using this rule, we can partition the students into circles of friends. To do this, implement a function networksO that takes two input arguments. The first is the number n of students in the class. We assume students are identified using integers0 through n-1. The second input argument is a list of tuple objects that define friends. For example, tuple (0, 2) defines students 0 and 2 as friends. Function networks(0 should print the partition of students into circles of friends as illustrated: networks(5, [(0, 1), (1, 2), (3, 4)]) Social network 0 is (0, 1,2 Social network 1 is 3,4;Explanation / Answer
Written in Python3
-------------------------------------------------------------------------------------------------------------------------
def store(frds,n): # For making graph
graph = {}
for i in range(n):
graph[i] = []
for x , y in frds:
graph[x].append(y)
return graph
def connection(graph, start): #This function will give one group of friends
visited, stack = set(), [start]
while stack:
vertex = stack.pop()
if vertex not in visited:
visited.add(vertex)
stack.extend(set(graph[vertex]) - visited)
return visited
def print_server(graph,n): # Printing of all connected friends in in different group
class_students=[]
i=0
k=0
while i!=n:
if i not in class_students:
temp = set(connection(graph,i))
class_students+=list(temp)
print('Social network '+ str(k) + ' is ' + str(set(sorted(temp))))
k+=1
i+=1
if __name__=='__main__':
n = 5 # strength of class (input)
frds = [(0,1),(1,2),(3,4)] # friends (input)
graph = store(frds,n)
print_server(graph,n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.