Skip to content

COVID Tracing Problem


You track the whereabouts of 100 individuals in a DataFrame called whereabouts. Each person has a corresponding list of place ids indicating the places they’ve visited in the recent week. You also track which places have been exposed to COVID-19 in a list called exposed 😷.

import numpy as np
import pandas as pd

# exposed places
exposed = [0,5,9]

# whereabouts of each person
generator = np.random.default_rng(2468)
Nplaces = 10
Npersons = 10
place_ids = np.arange(Nplaces)
visits = generator.choice(place_ids, size=3*Nplaces, replace=True)
split_idxs = np.sort(generator.choice(len(visits), size=9, replace=True))
whereabouts = pd.DataFrame({
    'person_id': range(Npersons),
    'places': [np.unique(x).tolist() for x in np.array_split(visits, split_idxs)]
})

print(whereabouts)
#    person_id              places
# 0          0        [3, 4, 5, 6]
# 1          1                  []
# 2          2                 [3]
# 3          3           [6, 8, 9]
# 4          4                 [3]
# 5          5  [0, 2, 5, 6, 7, 8]
# 6          6              [2, 7]
# 7          7        [0, 5, 8, 9]
# 8          8           [2, 7, 9]
# 9          9        [0, 5, 8, 9]

For each person, identify the places they visited which have been exposed. Make this a new list-column in whereabouts called exposures.


Try with Google Colab See the solution