Introduction

Hello, and welcome, code explorer! Today's journey takes us through the intricate paths within a 2-dimensional array, often likened to a game board. Our mission is to identify ideal spots for game piece placement. Sounds like an adventure, doesn't it? Let's embark!

Task Statement

Visualize a chessboard as a 2D array. Each cell contains either 'E' for an empty cell or 'P' for a cell that already has a piece.

Your task is to write a Python function named find_positions() that returns every empty cell where we could place a new piece and that new piece would have at least one legal move on its next turn.

A legal move means moving from the placed piece's cell to another empty cell that is immediately adjacent:

  • directly above, below, left, or right
  • not diagonally

Consider this 4x4 board for instance:

P E E P
E P E P
P E P P
P E P E

The function should render an output as: [(0, 1), (0, 2), (1, 2), (2, 1), (3, 1)]. This output represents the positions where a new piece can fit perfectly and then be able to move in the next turn.

Solution Building: Step 1

Stepping right into action, we start with an empty positions list to help us log the sought positions. Understanding the dimensions of our ‘board’ paves the way for defining boundaries in our exploration mission. Now, how does one determine the size of a Python list? The answer lies in Python's len() function.

def find_positions(board):
    positions = []
    rows, cols = len(board), len(board[0])
Solution Building: Step 2

With our boundary map, we begin our expedition across the board. We use two nested for loops to do this, traversing the entire board one cell at a time.

def find_positions(board):
    positions = []
    rows, cols = len(board), len(board[0])

    for i in range(rows):
        for j in range(cols):
            # ensuing exploration
Solution Building: Step 3

What's the plan for each cell, you ask? While exploring each cell, our trusty Python code inspects if the cell is empty. If confirmed, it then peeks into the neighbors in the up, down, left, right directions. If another vacant cell ('E') is spotted, we jot down the main cell's position in our result list.

def find_positions(board):
    positions = []
    rows, cols = len(board), len(board[0])

    for i in range(rows):
        for j in range(cols):
            if board[i][j] == 'E':
                if ((i > 0 and board[i-1][j] == 'E') or 
                (i < rows - 1 and board[i+1][j] == 'E') or 
                (j > 0 and board[i][j-1] == 'E') or 
                (j < cols - 1 and board[i][j+1] == 'E')):
                    positions.append((i, j))
    return positions

board = [
    ['P', 'E', 'E', 'P'],
    ['E', 'P', 'E', 'P'],
    ['P', 'E', 'P', 'P'],
    ['P', 'E', 'P', 'E']
]

print(find_positions(board))

# Prints [(0, 1), (0, 2), (1, 2), (2, 1), (3, 1)]
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal