def test_degrees(load_small_data): kevin_bacon = "102" tom_cruise = "129" tom_hanks = "158" emma_watson = "914612" a_few_good_men = "104257" apollo_13 = "112384" assert shortest_path(kevin_bacon, tom_cruise) == [(a_few_good_men, tom_cruise)] assert shortest_path(tom_cruise, kevin_bacon) == [(a_few_good_men, kevin_bacon)] assert shortest_path(tom_hanks, kevin_bacon) == [(apollo_13, kevin_bacon)] assert shortest_path(kevin_bacon, tom_hanks) == [(apollo_13, tom_hanks)] assert shortest_path(tom_hanks, tom_cruise) == [ (apollo_13, kevin_bacon), (a_few_good_men, tom_cruise), ] assert shortest_path(tom_cruise, tom_hanks) == [ (a_few_good_men, kevin_bacon), (apollo_13, tom_hanks), ] assert shortest_path(kevin_bacon, emma_watson) is None assert shortest_path(emma_watson, kevin_bacon) is None
import degrees as dg dg.load_data("large") dg.names dg.people dg.movies #n = dg.neighbors_for_person('102') source = '102' target = '1597' path = dg.shortest_path(source, target) if path is None: print("Not connected.") else: print(path) degrees = len(path) print(f"{degrees} degrees of separation.") path = [(None, source)] + path for i in range(degrees): person1 = dg.people[path[i][1]]["name"] person2 = dg.people[path[i + 1][1]]["name"] movie = dg.movies[path[i + 1][0]]["title"] print(f"{i + 1}: {person1} and {person2} starred in {movie}") #dg.main()
from degrees import shortest_path print(shortest_path(12, 12))
def get_shortest_path(self, directory, sourceName, targetName): load_data(directory) source = person_id_for_name(sourceName) target = person_id_for_name(targetName) return shortest_path(source, target)
def test_small_2(load_small_database): result = degrees.shortest_path("102", "398") possibilities = [[("112384", "158"), ("109830", "398")], [("112384", "641"), ("109830", "398")]] assert result in possibilities
def test_small_1(load_small_database): result = degrees.shortest_path("102", "129") assert result == [("104257", "129")]
def test_small_0(load_small_database): result = degrees.shortest_path("102", "102") assert result == []