Я думал в этой (не очень элегантной) логике, чтобы сохранить распределение:
def assure_2zeros(l):
for idx in range(len(l)):
# detect the problem when the ones are adjacent
if l[idx] == 1 and l[idx+1] == 1:
# check forward for a zero that could be realocated to split the ones
for i in range(idx+2, len(l)-1):
# check to not create other problems
if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
del l[i]
l.insert(idx+1, 0)
break
# if doesnt found any zero forward, check backward
else:
for i in range(idx-1, 0, -1):
if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
del l[i]
l.insert(idx+1, 0)
break
# detects the problem when there are one zero between the ones
if l[idx] == 1 and l[idx+2] == 1:
for i in range(idx+3, len(l)-1):
if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
del l[i]
l.insert(idx+1, 0)
break
else:
for i in range(idx-1, 0, -1):
if l[i] == 0 and l[i-1]== 0 and l[i+1]== 0:
del l[i]
l.insert(idx+1, 0)
break
return l
Обратите внимание, что когда есть два смежных, первый, если будет вставлять ноль между ними, тогда он будет вводитьво втором if и второй ноль будут вставлены.Однако, это терпит неудачу, когда есть два или один ноль в самом конце или начале списка.В операторах if можно добавить больше условий, но для вашего случая достаточно нулей, и я думаю, что это может сработать.