Add the in-progress exercise

This commit is contained in:
2026-08-11 20:32:37 -04:00
parent 22a8e3305d
commit 1c26ae7b86

View File

@@ -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))