A grid is a perfect starting point for many games (Chess, battleships, Candy Crush!).

Making a digital chessboard I think is an interesting way of visualising how loops can work together.

Your task is to write a function that takes two integers `rows` and `columns` and returns a chessboard pattern as a two dimensional array.

So `chessBoard(6,4)` should return an array like this:


    [
            ["O","X","O","X"],
            ["X","O","X","O"],
            ["O","X","O","X"],
            ["X","O","X","O"],
            ["O","X","O","X"],
            ["X","O","X","O"]
    ]

And `chessBoard(3,7)` should return this:


    [
        ["O","X","O","X","O","X","O"],
        ["X","O","X","O","X","O","X"],
        ["O","X","O","X","O","X","O"]
    ]

The white spaces should be represented by an: `'O'`

and the black an: `'X'`

The first row should always start with a white space `'O'`

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Ad-hoc

Starter code: def chess_board(rows, columns):
	

Solutions:
def chess_board(rows, columns):
	return [['OX'[(row + col) % 2] for col in range(columns)] for row in range(rows)]


def chess_board(rows, columns):
	ans = []
	for i in range(1, rows + 1, 1):
		l = []
		for j in range(i, columns + i, 1):
			if j % 2 != 0:
				l.append('O')
			else:
				l.append('X')
		ans.append(l)
	return ans


chess_board = lambda rows, cols: [['X' if (y + x) % 2 else 'O' for x in range(cols)] for y in range(rows)]


chess_board = lambda r, c: [['OX'[i + j & 1] for i in range(c)] for j in range(r)]


def chess_board(rows, columns):
	board = []
	for row in range(rows):
		if row % 2:
			board.append(['X' if not column % 2 else 'O' for column in range(columns)])
		else:
			board.append(['O' if not column % 2 else 'X' for column in range(columns)])
	return board


def chess_board(a, b):
	return [list('OXXO'[i % 2::2] * (b // 2 + 1))[:b] for i in range(a)]


from itertools import cycle

def chess_board(rows, columns):
	result = []
	for i in range(rows):
		grida = cycle(['O', 'X'])
		gridb = cycle(['X', 'O'])
		result.append([next(gridb) if i % 2 else next(grida) for x in range(columns)])
	return result


def chess_board(rows, columns):
	return [['OX'[(row + column) % 2] for column in range(columns)] for row in range(rows)]


def chess_board(rows, columns):
	line = ['X', 'O'] * columns
	return [line[:columns] if r % 2 else line[1:columns + 1] for r in range(rows)]


chess_board = lambda r, c: [['OX'[(i + j) % 2] for j in range(c)] for i in range(r)]