0

Someone can explain me this code??

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()) u = 0 for i in range(len(x.adj)): if i+1 == n: for j in x.adj[i]: if j == 1: u += 1 print(u)

23rd Aug 2022, 7:52 PM
Josué Varela
Josué Varela - avatar
1 Respuesta
+ 2
It is a graph represented by an adjacent list. The graph shows if two persons are friends. Now it is to enter the person and calculate the number of friends. If it is person i you count the number of 1 in the (i +1th) row. The class is coded 👍🏻 ... But finding number of friends seems to be coded by a beginner. Well...it seems to work, experience comes with doing.
24th Aug 2022, 5:12 AM
Oma Falk
Oma Falk - avatar