Lekcja – Graf nieskierowany
V = ['Princess', 'Dwarf1', 'Dwarf2', 'King', 'Queen', 'Hunter'] E = [ ['Princess', 'Dwarf1'], ['Princess', 'Dwarf2'], ['Dwarf1', 'Dwarf2'], ['Princess', 'King'], ['Princess', 'Queen'], ['Princess', 'Hunter'], ['King', 'Queen'], ['King', 'Hunter'], ['Queen','Hunter'] ] # who knows the king? def who_knows_person(person, E): friends=[] for p1, p2 in E: if p1 == person: friends.append(p2) elif p2 == person: friends.append(p1) return friends print(who_knows_person('Princess', E))
Lab
V = ['Princess', 'Dwarfs', 'Queen', 'King', 'Hunter'] E = [ [0, 1], [0, 2], [0, 3], [0, 4], [2, 3], [2, 4], [3, 4] ] # Who knows king? def who_knows_person(person, V, E): friends = [] for p1, p2 in E: if V[p1] == person: friends.append(V[p2]) elif V[p2] == person: friends.append(V[p1]) return friends print(who_knows_person('King', V, E))