SnIPPETsX
SnIPPETsX

SnIPPETsX › def main()

def main()

Python · updated Nov 12, 2026 · 39 lines

main.py


#    []
# [] [] []
#    []

def main():
  n = 5
  generate_nomino(n)

def generate_nomino(int n):
  new_grid = int[n][n]
  if n == 0:
    return
  int[0][0] = 1
  for i in range(n):
    r, c = calculate_next_square(new_grid).get()
    new_grid[r][c] = 1
  return new_grid

def calculate_next_square(int[][] grid):
  possible_next_moves = set()
  for each r in grid:
    for each c in grid[r]:
      if c > 0:
        if grid[r][c-1] == 0:
          possible_next_moves.add((r, c-1))
      if c < len(grid[0])-1:
        if grid[r][c+1] == 0:
          possible_next_moves.add((r, c-1))
      if r < len(grid[0])-1:
        if grid[r+1][c] == 0:
          possible_next_moves.add((r+1, c))
      if r < 0:
        if grid[r-1][c] == 0:
          possible_next_moves.add((r+1, c))
  return possible_next_moves
      
if __name__ == "main":
  main()

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.