def match_videos_and_captions(videos, sentences):
    """Maps captions to videos and splits the data into train/valid/test.

    arguments:
        videos: a list of dicts, where each dict describes a video
        sentences: a list of dicts, where each dict describes a sentence

    returns:
        a dict that maps from split name to a list of videos, where each video
        is a dict that contains a field "url" that is the video's url, and a
        field "captions", a list of strings where each string is a possible
        caption of the video.
    """
    dataset_metadata = {}
    video_id_to_split_index_pair = {}

    for video in videos:
        split = video["split"]
        id_ = video["video_id"]

        if split not in dataset_metadata:
            dataset_metadata[split] = []

        video["captions"] = []
        video_id_to_split_index_pair[id_] = (
        	split, 
        	len(dataset_metadata[split])
        )

        dataset_metadata[split].append(video)

    for sentence in sentences:
        caption_text = sentence["caption"]
        video_id = sentence["video_id"]
        
        video_split, video_index = video_id_to_split_index_pair[video_id]
        video = dataset_metadata[video_split][video_index]

        video["captions"].append(sentence["caption"])

    return dataset_metadata