def windows_union(windows):
    """Given a list of (beginning, ending), return a minimal version that contains the same ranges.

    :rtype: list

    """
    def fix_overlap(left, right):
        if left == right:
            return [left]
        assert left[0] < right[0]
        if left[1] >= right[0]:
            if right[1] > left[1]:
                return [(left[0], right[1])]
            else:
                return [left]
        return [left, right]

    if len(windows) == 1:
        return windows
    none_left = []
    none_right = []
    otherwise = []
    for window in windows:
        if window[0] is None:
            none_left.append(window)
        elif window[1] is None:
            none_right.append(window)
        else:
            otherwise.append(window)

    res = []
    otherwise.sort()
    for window in none_left:
        if not res:
            res.append(window)
            continue
        res.extend(fix_overlap(res.pop(), window))
    while otherwise:
        window = otherwise.pop(0)
        if not res:
            res.append(window)
            continue
        res.extend(fix_overlap(res.pop(), window))
    for window in none_right:
        if not res:
            res.append(window)
            continue
        res.extend(fix_overlap(res.pop(), window))
    return res
def windows_union ( windows ) : def fix_overlap ( left , right ) : if left == right : return [ left ] assert left [ 0 ] < right [ 0 ] if left [ 1 ] >= right [ 0 ] : if right [ 1 ] > left [ 1 ] : return [ ( left [ 0 ] , right [ 1 ] ) ] else : return [ left ] return [ left , right ] if len ( windows ) == 1 : return windows none_left = [ ] none_right = [ ] otherwise = [ ] for window in windows : if window [ 0 ] is None : none_left . append ( window ) elif window [ 1 ] is None : none_right . append ( window ) else : otherwise . append ( window ) res = [ ] otherwise . sort ( ) for window in none_left : if not res : res . append ( window ) continue res . extend ( fix_overlap ( res . pop ( ) , window ) ) while otherwise : window = otherwise . pop ( 0 ) if not res : res . append ( window ) continue res . extend ( fix_overlap ( res . pop ( ) , window ) ) for window in none_right : if not res : res . append ( window ) continue res . extend ( fix_overlap ( res . pop ( ) , window ) ) return res
Given a list of (beginning, ending), return a minimal version that contains the same ranges.

    :rtype: list