SnIPPETsX
SnIPPETsX

SnIPPETsX › n -> output random instance of valid n-omnio

n -> output random instance of valid n-omnio

Python · updated Nov 11, 2026 · 35 lines

main.py

# n -> output random instance of valid n-omnio
import random

def generate_n_omino(n: int):
  grid = [['O' for _ in range(n)] for _ in range(n)]

  grid[n // 2][n // 2] = 'X'
  counter = 0

  while counter < n:
    candidate_points = []
    for row in grid:
      for col in grid:
        if is_spreadable(grid, row, col):
          candidate_points.append((row, col))

    
    random_candidate = random.randint(0, len(candidate_points) - 1)
    candidate_points[random_candidate] = 'X'
    
    counter += 1

def is_spreadable(grid, row, col):
  piece_above = get_grid_value(grid, row + 1, col) == 'X'
  piece_below = get_grid_value(grid, row - 1, col) == 'X'
  piece_on_left = get_grid_value(grid, row, col - 1) == 'X'
  piece_on_right = grid_grid_value(grid, row, col + 1) == 'X'
  piece_nearby = piece_above or piece_below or piece_on_left or piece_on_right
  
  return get_grid_value(grid, row, col) == 'O' and piece_nearby
  
def get_grid_value(grid, row, col):
    if 0 <= row < len(grid) and 0 <= col < len(grid):
      return grid[row][col]
    return 'B'

Run this Python snippet

Open this snippet on SnIPPETsX to run the Python code in your browser, edit it together with others in real time, or fork it into your own copy. No install or account is needed.