+ 2

Counting the number of connections in an adj matrix with 5 vertices.....Help! I'm a wee lost..

Hello! I'm trying to complete this code....[problem in Python Data Structure course, entitled, "let's connect"]. I understand I have to count the connections, thus the number of "1"s in the adjacency matrix. I'm a bit lost, would someone help out, please? class X(): def __init__(self, size): self.adj = [ [0] * size for i in range(size)] self.size = size def add_friend(self, x, y): if x > self.size or y > self.size or x < 0 or y < 0: print("Error") else: self.adj[x-1][y-1] = 1 self.adj[y-1][x-1] = 1 def remove_friend(self, x, y): if x > self.size or y > self.size or x < 0 or y < 0: print("Error") else: self.adj[x-1][y-1] = 0 self.adj[y-1][x-1] = 0 x = X(5) x.add_friend(1, 3) x.add_friend(1, 5) x.add_friend(2, 5) x.add_friend(2, 4) x.add_friend(4, 5) n = int(input()) #your code goes here count=0 for n in range(5): if n.adj[x-1][y-1] = 1 and n.adj[y-1][x-1] = 1 count+=1 print(count)

7th Mar 2022, 12:29 PM
Cathyboum
1 Odpowiedź