manish.sharma0201cs
BAN USERExample
friends_data_dict = {
a: [b,c]
b: [c,d]
d: [e,f]
p: [f,i]
j: [k,l]
}
a and b have 1 degree friendship
a and d are 2 degree friends
a and f are 3 degree friends
a and i are 4 degree friends
a and l are not friends
class Friend:
def __init__(self, name):
self.name = name
self.friends = []
def get_degree_of_friendship(friend_one: Friend, friend_two: Friend):
“””
say friend a is friend_one and f is friend_two
“””
friends_identified = _set()
friends_identified .add(friend_one.friends) #Here friends_identified = [b,c]
friends_queue_holder = []
friends_queue_holder.append(friend_one.friends)
friends_ordered_set = get_friend_sequence(
friend_two,
friends_identified,
friends_queue_holder
)
degree = len(friends_ordered_set)
print(“%s and %s are %s degree friends” % (friend_one, friend_two, degree))
def get_friend_sequence():
while friends_queue_holder:
friend = friends_queue_holder.pop(0)
if friend_two in friends_data_dict[friend]:
return friends_identified
friends_identified.add(friends_data_dict[friend])
friends_queue_holder = friends_data_dict[friend]
- manish.sharma0201cs September 05, 2021