Add the in-progress exercise
This commit is contained in:
@@ -13,16 +13,32 @@ sample_graph = {
|
||||
'Mallory': set(['Carol', 'Grace'])
|
||||
}
|
||||
|
||||
# This is an in-progress exercise
|
||||
|
||||
def nth_degree_connections(
|
||||
graph: dict[str, set[str]],
|
||||
starting_vertex: str,
|
||||
max_n: int
|
||||
):
|
||||
# TODO
|
||||
pass
|
||||
checkinglist=[[starting_vertex,0]]
|
||||
listvisited=list()
|
||||
x=[0,0]
|
||||
while len(listvisited)!=len(graph) and checkinglist!=[] and x[1]<max_n:
|
||||
x=checkinglist[0]
|
||||
if x[0] not in listvisited:
|
||||
listvisited.append(x)
|
||||
for y in graph[x[0]]:
|
||||
if y not in listvisited:
|
||||
listvisited.append(y)
|
||||
checkinglist.append([y,x[1]+1])
|
||||
checkinglist.pop(0)
|
||||
return listvisited
|
||||
|
||||
|
||||
print(nth_degree_connections(sample_graph, 'Mallory', 2))
|
||||
print(nth_degree_connections(sample_graph, 'Alice', 4))
|
||||
print(nth_degree_connections(sample_graph, 'Ivan', 1))
|
||||
print(nth_degree_connections(sample_graph, 'Grace', 0))
|
||||
print(nth_degree_connections(sample_graph, 'Carol', 100))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user