+ 6

26.2 Practice Let's Connect (Pyhton Data Structures)

Problem: Graphs You are making a social network called X. Connections between the users are stored as a graph. The given code declares an X class with its add_friend() method and creates some connections for 5 users. You need to take a number as input and output the number of connections of the corresponding user. Each user's connections are stored in the adj matrix. 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 I hate doing this, but I'm just not sure what to do for this

16th Sep 2021, 7:58 PM
Cory Peitsch
Cory Peitsch - avatar
9 Answers
0
Another one you could try is this one: 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 def get_connections(self, user): if user > self.size or user < 1: print("Error") else: return sum(self.adj[user - 1]) 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("Enter the user number: ")) connections = x.get_connections(n) print("Number of connections:", connections)
31st May 2023, 11:39 AM
Khanyisani Kumalo
Khanyisani Kumalo - avatar
+ 11
#your code goes here a =0 for i in range (len(x.adj)): if i+1==n: for j in x.adj[i]: if j==1: a += 1 print(a)
26th May 2022, 11:34 AM
P.G.P.Perera
+ 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] = 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 a =0 for i in range (len(x.adj)): if i+1==n: for j in x.adj[i]: if j==1: a += 1 print(a)
14th Nov 2022, 8:03 AM
Pooja Patel
Pooja Patel - avatar
+ 1
cory p what part are you not sure about ? In the 26.1 section 3rd question the details may be helpful if you look at the "try it"
16th Sep 2021, 8:37 PM
BroFar
BroFar - avatar
0
ok, I'm going to look at it again
16th Sep 2021, 9:15 PM
Cory Peitsch
Cory Peitsch - avatar
0
I think this is the part 26.1 Q3 I need to incorporate, right?: def display(self): for row in self.adj: print() for val in row: print('{:4}'.format(val),end="")
16th Sep 2021, 9:22 PM
Cory Peitsch
Cory Peitsch - avatar
0
Alright, thank you for the guidance. I'll keep working on it :)
16th Sep 2021, 9:36 PM
Cory Peitsch
Cory Peitsch - avatar
0
Could anyone explain to me what is the input should be? (How does the input number affect the number of connections of the corresponding users?) (because I could not figure out what is the relation of the input with the number of connections of the corresponding users)
13th Feb 2023, 9:05 AM
Wan Qi
Wan Qi - avatar
- 1
cory p Yes something along those lines -
16th Sep 2021, 9:25 PM
BroFar
BroFar - avatar