给定一个图表,程序按字母顺序显示可能的朋友
Given a graph, program that presents, in alphabetical order, the probable friends
我是python的初学者,我在解决图形上的练习时遇到了问题,练习如下:
给定一个表示社交网络关系的图表,创建一个程序,按字母顺序列出“Mussum”可能的朋友。要找到“Mussum”可能的朋友,请考虑以下规则:至少有 3 个共同朋友的两个断开连接的人彼此成为朋友的可能性很高,因此很可能是朋友。
输入:
输入的第一行表示图中有多少个n个顶点(1≤n≤100)。接下来n行每行表示一个顶点的信息,格式为:id,A,v1,v2,⋯,vA,其中id为标识顶点的字符串,A是连接到该顶点的边数,每条vi≠id是标识一个顶点的字符串与 id 相邻。第一个 id 总是 Mussum 的。
退出:
按字母顺序显示 Mussum 可能的朋友,如果 none,打印消息“没有朋友”
例如:
输入:
6
Mussum 3 Didi Dede Zacarias
Zacarias 5 Didi Dede Macale Mussum Sargento
Dede 5 Didi Macale Mussum Zacarias Sargento
滴滴 5 Mussum Dede Macale Zacarias Sargento
Sargento 4 Didi Dede Zacarias Macale
Macale 4 Sargento Didi Dede Zacarias
输出:
马卡莱
萨金托
输入:
4
Mussum 3 Sorvetao Conrado Dede
Dede 2 Sorvetao Mussum
Sorvetao 3 Mussum Conrado Dede
孔拉多 2 Mussum Sorvetao
输出:
“没有朋友”
到目前为止,我对这个问题的代码是这样的,因为我无法解决这个问题。代码:
graph = {}
for _ in range(int(input())):
v, A, *neighbor = input().split()
graph[v] = neighbor
friend_of_friends = {}
for friend in graph['Mussum']:
for friends_neighbor in graph[friend]:
if friends_neighbor in friend_of_friends:
friend_of_friends[friends_neighbor] += 1
else:
friend_of_friends[friends_neighbor] = 1
你的开局不错。 friends_of_friends
词典包含获得结果所需的所有信息。 friends_of_friends
字典包含每个人与 Mussum 的 his/her 个共同朋友的数量。然后你只需要检查号码是否符合要求然后添加一些逻辑以确保你 return 可能的朋友按字母顺序排列并且你例如不报告 Mussum 作为他自己可能的朋友。可能的解决方案可能如下所示:
graph = {}
for _ in range(int(input())):
v, A, *neighbor = input().split()
graph[v] = neighbor
friend_of_friends = {}
for friend in graph['Mussum']:
for friends_neighbor in graph[friend]:
if friends_neighbor in friend_of_friends:
friend_of_friends[friends_neighbor] += 1
else:
friend_of_friends[friends_neighbor] = 1
likely_friends = []
for possible_likely_friend, mutual_friends_count in friend_of_friends.items():
# Skip checking if Mussum is a likely friend of himself
if possible_likely_friend == 'Mussum':
continue
# Do not report people that are already friends with Mussum as likely friends
if possible_likely_friend in graph['Mussum']:
continue
# If the person has at least 3 mutual friends with Mussum, add the person to the list
if mutual_friends_count >= 3:
likely_friends.append(possible_likely_friend)
# If there are some likely friends, first sort them alphabetically and then output
# them each on one line. If no likely friends, output "No friends".
if likely_friends:
likely_friends.sort()
for likely_friend in likely_friends:
print(likely_friend)
else:
print("No friends")
我是python的初学者,我在解决图形上的练习时遇到了问题,练习如下:
给定一个表示社交网络关系的图表,创建一个程序,按字母顺序列出“Mussum”可能的朋友。要找到“Mussum”可能的朋友,请考虑以下规则:至少有 3 个共同朋友的两个断开连接的人彼此成为朋友的可能性很高,因此很可能是朋友。
输入:
输入的第一行表示图中有多少个n个顶点(1≤n≤100)。接下来n行每行表示一个顶点的信息,格式为:id,A,v1,v2,⋯,vA,其中id为标识顶点的字符串,A是连接到该顶点的边数,每条vi≠id是标识一个顶点的字符串与 id 相邻。第一个 id 总是 Mussum 的。
退出:
按字母顺序显示 Mussum 可能的朋友,如果 none,打印消息“没有朋友”
例如:
输入:
6
Mussum 3 Didi Dede Zacarias
Zacarias 5 Didi Dede Macale Mussum Sargento
Dede 5 Didi Macale Mussum Zacarias Sargento
滴滴 5 Mussum Dede Macale Zacarias Sargento
Sargento 4 Didi Dede Zacarias Macale
Macale 4 Sargento Didi Dede Zacarias
输出:
马卡莱
萨金托
输入:
4
Mussum 3 Sorvetao Conrado Dede
Dede 2 Sorvetao Mussum
Sorvetao 3 Mussum Conrado Dede
孔拉多 2 Mussum Sorvetao
输出:
“没有朋友”
到目前为止,我对这个问题的代码是这样的,因为我无法解决这个问题。代码:
graph = {}
for _ in range(int(input())):
v, A, *neighbor = input().split()
graph[v] = neighbor
friend_of_friends = {}
for friend in graph['Mussum']:
for friends_neighbor in graph[friend]:
if friends_neighbor in friend_of_friends:
friend_of_friends[friends_neighbor] += 1
else:
friend_of_friends[friends_neighbor] = 1
你的开局不错。 friends_of_friends
词典包含获得结果所需的所有信息。 friends_of_friends
字典包含每个人与 Mussum 的 his/her 个共同朋友的数量。然后你只需要检查号码是否符合要求然后添加一些逻辑以确保你 return 可能的朋友按字母顺序排列并且你例如不报告 Mussum 作为他自己可能的朋友。可能的解决方案可能如下所示:
graph = {}
for _ in range(int(input())):
v, A, *neighbor = input().split()
graph[v] = neighbor
friend_of_friends = {}
for friend in graph['Mussum']:
for friends_neighbor in graph[friend]:
if friends_neighbor in friend_of_friends:
friend_of_friends[friends_neighbor] += 1
else:
friend_of_friends[friends_neighbor] = 1
likely_friends = []
for possible_likely_friend, mutual_friends_count in friend_of_friends.items():
# Skip checking if Mussum is a likely friend of himself
if possible_likely_friend == 'Mussum':
continue
# Do not report people that are already friends with Mussum as likely friends
if possible_likely_friend in graph['Mussum']:
continue
# If the person has at least 3 mutual friends with Mussum, add the person to the list
if mutual_friends_count >= 3:
likely_friends.append(possible_likely_friend)
# If there are some likely friends, first sort them alphabetically and then output
# them each on one line. If no likely friends, output "No friends".
if likely_friends:
likely_friends.sort()
for likely_friend in likely_friends:
print(likely_friend)
else:
print("No friends")