import collections


def find_container(intersections):
    """ Returns the container node.
    
        Parameters
        ----------
        intersections: List[Intersection]
            Full list of intersections of ray with a scene.
    
        Returns
        -------
        Node
            The container node

        Example
        -------
        >>> intersections = scene.intersections(ray.position, ray.directions)
        >>> container = find_container(intersections)
    """
    if len(intersections) == 1:
        return intersections[0].hit
    count = collections.Counter([x.hit for x in intersections]).most_common()
    candidates = [x[0] for x in count if x[1] == 1]
    pairs = []
    for intersection in intersections:
        node = intersection.hit
        if node in candidates:
            pairs.append((node, intersection.distance))
    # [(node, dist), (node, dist)... ]
    pairs = sorted(pairs, key=lambda tup: tup[1])
    containers, _ = zip(*pairs)
    container = containers[0]
    return container