Задачу о клумбе верно решили:
— Cool and Fun Python (@Python3k);
— @Cheshir78;
— Лев Выскубов;
— @patriarch_chesslovo;
— @vsugoyak;
— @sidor0912;
Кратчайшие верные решения предложили Лев Выскубов:
def is_fitting(flowerbed, n):
flowerbed = [0] + flowerbed + [0] # Чтобы на краю можно было цветочек посадить :-)
k = 0
for slot in range(1, len(flowerbed) - 1):
if not any(flowerbed[(slot - 1) : (slot + 2)]):
flowerbed[slot] = 1
k += 1
return k >= n
и @patriarch_chesslovo:
def is_fitting(flowerbed, required_slots):
is_prev_slot_filled = sum(flowerbed[0:2]) == 0
vacant_slots = int(is_prev_slot_filled)
for i in range(1, len(flowerbed)):
is_curr_slot_filled = not is_prev_slot_filled and sum(flowerbed[i-1:i+2]) == 0
vacant_slots += int(is_curr_slot_filled)
is_prev_slot_filled = is_curr_slot_filled
if vacant_slots >= required_slots:
return True
return vacant_slots >= required_slots
Отдельное спасибо @patriarch_chesslovo за описание пограничных случаев (одноместная клумба) и проверку некоторых решений.
Присоединяйтесь: одна задача в неделю — это вполне оптимальная нагрузка, а навык решать задачи все равно будет расти.
#задача
@zen_of_python