2. Likes

Consider a scenario of a social network of people where anyone may “give a like” to anyone else. Graphically we have a number of nodes representing people and a number directed arrows representing a like given by the origin to the target. See the following example:

alternate text

This information can be represented in Python as a dictionary where the keys are person names in the network (str) and the associated value is the list of people names (list of str) to whom that person gave a like.

For example:


>>> likesD_1 = {
... 'kiku': ['laia', 'berta'],
... 'laia': ['pol', 'kiku'],
... 'nil': ['kiku', 'pol'],
... 'pol': ['berta', 'laia', 'kiku'],
... 'berta': ['kiku'],
... 'carla': ['laia', 'berta', 'kiku'],
... }

Note

We assume that all likes involve people appearing as a key in the dictionary, hence there are no likes to people not belonging to the network.

You are required to implement the following functions and deliver them in the module likes (file likes.py).


The first function is:

compute_likedby_dict(likesD)

such that

given likesD (dict) a likes dictionary as described above. returns a new dict where:

  1. The keys are all the names (str) of people in the network.

  2. The value associated to each key is a list with all the names of people that has given a like to the key person. The names must be alphabetically ordered.

The input dict should not be modified.

Note

The new dict must also include the people who did not receive any like, whose value will consequently be the empty list.

For example, the expected outpout for the above likes dict would be:


>>> correctD = {
... 'kiku': ['berta', 'carla', 'laia', 'nil', 'pol'],
... 'laia': ['carla', 'kiku', 'pol'],
... 'berta': ['carla', 'kiku', 'pol'],
... 'pol': ['laia', 'nil'],
... 'nil': [],
... 'carla': []}

Doctests are available in the compute_likedby_dict.test file.


The second function is:

compute_matches_list(langD)

such that

given likesD (dict) a likes dictionary as described above.

returns a list of pairs of people (tuple) that matched. Both, the names inside each tuple and the list of tuples must be alphabetically ordered.

For example, the expected outpout for the above example dict would be:


>>> correctL = [('berta', 'kiku'), ('kiku', 'laia'), ('laia', 'pol')]

Doctests are available in the compute_matches_list.test file.