這(我想)做什麼@Martijn沒有和具有不包括連續兩天的額外收益(例如,如果你不想8休息日連續):
#Day selector
import random
Ndays = 8
daysoff = range(1,25)
concurrent_tol = 3
while True:
cntr = 0
sample = random.sample(daysoff, Ndays)
sample.sort()
for i in range(1,Ndays-1):
if abs(sample[i]-sample[i-1]) == 1:
cntr +=1
if abs(sample[i]-sample[i+1]) == 1:
cntr +=1
if cntr<concurrent_tol:
print "Found a good set of off-days :"
print sample
break
else:
print "Didn't find a good set, trying again"
print sample
輸出示例:
Didn't find a good set, trying again
[3, 4, 5, 6, 7, 8, 9, 11]
Didn't find a good set, trying again
[1, 5, 6, 7, 12, 14, 19, 20]
Didn't find a good set, trying again
[4, 5, 7, 9, 11, 15, 16, 20]
Didn't find a good set, trying again
[3, 4, 6, 7, 12, 13, 14, 23]
Didn't find a good set, trying again
[1, 7, 10, 12, 15, 16, 17, 22]
Didn't find a good set, trying again
[5, 7, 8, 11, 17, 18, 19, 23]
Didn't find a good set, trying again
[3, 8, 11, 12, 13, 15, 17, 21]
Didn't find a good set, trying again
[2, 5, 7, 8, 9, 12, 13, 21]
Found a good set of off-days :
[1, 2, 5, 12, 15, 17, 19, 20]
這也有看起來醜陋的額外好處。請注意,可能的日期是1-24天,如daysoff中所定義。
你是什麼意思的「分佈應儘可能」?你的意思是像週末的相同數量等?或者你的意思是整個月的密度大致相同?或者是其他東西? – Bakuriu
請參閱我更新的問題。 – alwbtc
@alwbtc什麼使一個分配好,另一個不好?你可以定義一些函數或者一些數學啓發式,可以分辨好的和不好的分佈之間的區別嗎? –