0

Where am I wrong? I expect when n= 3 output be 1, when n=2 I get 2

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] = 1 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 total_connections = 0 for i in x.adj: for j in i: if j: total_connections += 1 print(total_connections*5//25)

18th Jul 2022, 5:08 PM
Samuel Kamama
Samuel Kamama - avatar
3 Answers
+ 3
You can link your code like this: Go to Code section, click +, select the programming language, insert your code, save. Come back to the thread, click +, Insert Code, sort for My Code Bits, select your code. This way, your code is more readable and other users can test it.
18th Jul 2022, 5:20 PM
Lisa
Lisa - avatar
+ 1
Did u understand the adj list? To get correct result, you need the sum of the nth row Or... as u did... count the 1s in the nth row. And do what Lisa explained. It will increase your chances to get an answer
18th Jul 2022, 6:19 PM
Oma Falk
Oma Falk - avatar
0
Pls do not post the same question twice. It makes things messy! https://www.sololearn.com/discuss/3062046/?ref=app
19th Jul 2022, 5:11 AM
Emerson Prado
Emerson Prado - avatar