Section #8 Solutions

Task 1: Find Missing Friends

                    
                        def find_missing_friends(filename):
   """
   Given data, find and print the list the names of
    the friends who are mentioned but are missing from the network.
    >>> input_dict = json.load(open("data.json"))
    >>> find_missing_friends(input_dict)
    ['Michael', 'Koa', 'Haipeng', 'Eli', 'Kavita', 'William', 'Jose', 'Nika']
    """

    data = input_dict["network"]
    missing_friends = []
    for person in data:
        person_data = data[person]
        persons_friends = person_data["friends"]
        for friend in persons_friends:
            name = friend["name"]
            if name not in data and name not in missing_friends:
                missing_friends.append(name)
    return missing_friends
                    
                

Task 2: Calculate Total Friendship Years

            
def calculate_total_years(filename, person):
    """
    Given data and person,
    calculate and print the total number of years a given person has been friends with their friends.

    >>> input_dict = json.load(open('data.json'))
    >>> calculate_total_years(input_dict, "Sabino")
    Sabino has a total of 10 years of friendship.

    >>> calculate_total_years(input_dict, "Langston")
    Langston is not in the network.
    """
    data = input_dict["network"]

    # Check if the person is in the network
    if person not in data:
        print(f"{person} is not in the network.")
        return

    # Calculate the total years of friendship
    total_years = 0
    person_dict = data[person]
    for friend in person_dict["friends"]:
        total_years += friend["years_friends"]

    print(f"{person} has a total of {total_years} years of friendship.")