从更大的给定边界创建一组连续的非重叠边界

creating a set of consecutive non-overlapping bounds from a larger given bounds

如果给定的边界为 (40,260) 并且需要将其分成 4 个相等的较小的连续非重叠边界(即,(40,95),(96,150),(151,205),(206,260)) 我尝试了以下方法。

def range_non_overlapping(range_left, range_right, range_per_cut, cut_num)  
    ranges=[]
    for i in range(cut_num):
            ranges.append(range_left + i * range_per_cut)
            ranges.append(min(range_right, (range_left + (i + 1) * range_per_cut))) 
    return ranges

但以下代码会导致边界 [40,95,95,150,150,205,205,260] 上的重叠区域(从列表开头开始的对是较小的边界)。我需要创建 [40,95,96,150,151,205,206,260]

如果端点是包含的,那么你不能得到 4 个相等的区域,因为大小 (221) 不是 4 的倍数。我认为你想要 (40,94),(95,149), (150,204), (205,260)),您可以通过以下方式完成:

def divide( left, right, cuts ):
    delta = (right-left+1) // cuts
    out = []
    for i in range(cuts):
        out.append( left )
        left += delta
        out.append( left - 1 )
    out[-1] = right
    return out

print( divide( 40, 260, 4 ) )

这些区域是 55、55、55 和 56。

def chunknize(start, stop, num):
    step = (stop - start) / num
    return [
        (start + i * step + (0 if i == 0 else 1),
        start + (i + 1) * step)
        for i in range(num)]
    
print(chunknize(40, 260, 4))
# [(40.0, 95.0), (96.0, 150.0), (151.0, 205.0), (206.0, 260.0)]