38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import random
|
|
from typing import List
|
|
|
|
def generate_flower_planting_case(length: int = 10, chance: float = 0.3) -> List[int]:
|
|
"""
|
|
Generates a flowerbed array of 0s and 1s.
|
|
0: Empty, 1: Planted.
|
|
Ensures no adjacent 1s (valid initial state).
|
|
"""
|
|
flowerbed = [0] * length
|
|
for i in range(length):
|
|
if random.random() < chance:
|
|
# Check constraints
|
|
prev_empty = (i == 0) or (flowerbed[i-1] == 0)
|
|
# Since we fill left-to-right, we only check left neighbor carefully.
|
|
# But we must also ensure we don't invalidate a future planting?
|
|
# Actually, just randomly placing 1s might create invalid state (1,1).
|
|
# So we place 1 only if i-1 is 0.
|
|
if prev_empty:
|
|
flowerbed[i] = 1
|
|
|
|
# Double check right neighbor constraint (though we fill sequentially,
|
|
# randomness might need a second pass or careful construction.
|
|
# Simplified: construction guarantees i-1 is 0.
|
|
# But what if we place at i, and then at i+1?
|
|
# The loop above allows that. Let's fix logic.
|
|
|
|
flowerbed = [0] * length
|
|
i = 0
|
|
while i < length:
|
|
if random.random() < chance:
|
|
flowerbed[i] = 1
|
|
i += 2 # Skip next spot to maintain validity
|
|
else:
|
|
i += 1
|
|
|
|
return flowerbed
|