def merge_periods(data):
    """
    Merge periods to have better continous periods.
    Like 350-450, 400-600 => 350-600

    :param data: list of periods
    :type data: list
    :return: better continous periods
    :rtype: list
    """
    newdata = sorted(data, key=lambda drange: drange[0])
    end = 0
    for period in newdata:
        if period[0] != end and period[0] != end - 1:
            end = period[1]
    dat = newdata
    new_intervals = []
    cur_start = None
    cur_end = None
    for dt_start, dt_end in dat:
        if cur_end is None:
            cur_start = dt_start
            cur_end = dt_end
            continue
        elif cur_end >= dt_start:
            cur_end = dt_end
        else:
            new_intervals.append((cur_start, cur_end))
            cur_start = dt_start
            cur_end = dt_end
    new_intervals.append((cur_start, cur_end))
    return new_intervals
