Python 如何检查周期范围的重叠(重叠的年度重复周期)
我试图找到一个优雅的算法来检查两年周期是否重叠。这一时期与年份无关,但可以预期一年总是闰年 例如,周期A=(3月1日至5月1日)和周期B=(4月1日至9月1日)重叠。 此外,A期=(10月1日至2月1日)和B期=(1月1日至3月1日)重叠 然而,我发现这比我预期的要困难得多。复杂性来自于跨越年底的时期 我已经找到了一个有效的解决方案(请参见下面的Python 如何检查周期范围的重叠(重叠的年度重复周期),python,range,cycle,overlap,Python,Range,Cycle,Overlap,我试图找到一个优雅的算法来检查两年周期是否重叠。这一时期与年份无关,但可以预期一年总是闰年 例如,周期A=(3月1日至5月1日)和周期B=(4月1日至9月1日)重叠。 此外,A期=(10月1日至2月1日)和B期=(1月1日至3月1日)重叠 然而,我发现这比我预期的要困难得多。复杂性来自于跨越年底的时期 我已经找到了一个有效的解决方案(请参见下面的doesOverlap(a,B)方法),但我发现它很难实现 # for the rest of the MWE context code, see fu
doesOverlap(a,B)
方法),但我发现它很难实现
# for the rest of the MWE context code, see further
# WORKING, but a bit convulted
def doesOverlap(A, B):
'''returns True if yearly period A and B have overlapping dates'''
# list to track if day in year is part of a period A
# (this could probably be done a bit cheaper with a dictionary of tuples, but not relevant for my question)
yeardayCovered = [False for x in range(366)] # leap year
# mark the days of A
for d in range(A.start, A.start + A.length):
yeardayCovered[d % 366] = True
# now check each of the days in B with A
for d in range(B.start, B.start + B.length):
if yeardayCovered[d % 366]:
return True
return False
我相信这应该是可能的,用更少的检查和更优雅。我尝试将其中一个开始日期设置为零偏移,应用一些模运算符,然后进行常规(非循环)范围重叠检查()。但我并没有在所有的测试用例中都使用它
#NOT WORKING CORRECTLY!!
def doesOverlap(A, B):
'''determines if two yearly periods have overlapping dates'''
Astart = A.start
Astop = A.stop
Bstart = B.start
Bstop = B.stop
# start day counting at Astart, at 0
offset = Astart
Astart = 0
Astop = (Astop - offset) % 366
Bstart = (Bstart - offset) % 366
Bstop = (Bstop - offset) % 366
# overlap?
# https://stackoverflow.com/a/13513973
return (Astart <= Bstop and Bstart <= Astop)
您可以将跨年底的时段划分为两个时段,然后对这两个时段进行比较。这将产生一个非常简单的递归函数,不需要任何特定于Python的特性:
def overlap(start_a, stop_a, start_b, stop_b):
if start_a > stop_a:
return overlap(start_a, 365, start_b, stop_b) or overlap(0, stop_a, start_b, stop_b)
elif start_b > stop_b:
return overlap(start_a, stop_a, start_b, 365) or overlap(start_a, stop_a, 0, stop_b)
else:
return start_a <= stop_b and start_b <= stop_a
def doesOverlap(A, B):
return overlap(A.start, A.stop, B.start, B.stop)
def重叠(启动a、停止a、启动b、停止b):
如果开始>停止:
返回重叠(开始a、365、开始b、停止b)或重叠(0、停止a、开始b、停止b)
elif开始>停止:
返回重叠(开始a、停止a、开始b、365)或重叠(开始a、停止a、0、停止b)
其他:
返回开始位置adef重叠(a0、a1、b0、b1):
#首先,我们从每年的“循环”中“提升”间隔
#在以下情况下,通过调整结束日期调整时间“线”
#在开始日期之前结束。。。
如果a1b0且a0b0和a0+365b0和a0-365
啊,这就是我要找的!我已经完成了从循环到线性的提升,但是向前/向后移动是我没有想到的。很好的解决方案,尤其是递归
def overlap(start_a, stop_a, start_b, stop_b):
if start_a > stop_a:
return overlap(start_a, 365, start_b, stop_b) or overlap(0, stop_a, start_b, stop_b)
elif start_b > stop_b:
return overlap(start_a, stop_a, start_b, 365) or overlap(start_a, stop_a, 0, stop_b)
else:
return start_a <= stop_b and start_b <= stop_a
def doesOverlap(A, B):
return overlap(A.start, A.stop, B.start, B.stop)
def overlap(a0, a1, b0, b1):
# First we "lift" the intervals from the yearly "circle"
# to the time "line" by adjusting the ending date if
# ends up before starting date...
if a1 < a0: a1 += 365
if b1 < b0: b1 += 365
# There is an intersection either if the two intervals intersect ...
if a1 > b0 and a0 < b1: return True
# ... or if they do after moving A forward or backward one year
if a1+365 > b0 and a0+365 < b1: return True
if a1-365 > b0 and a0-365 < b1: return True
# otherwise there's no intersection
return False