Skip to content

Chess Board Cell Color

Given two cells on the standard chess board, determine whether they have the same color or not.

Example

  • For cell1 = "A1" and cell2 = "C3", the output should be

    chess_board_cell_color(cell1, cell2) = true
    Image Credit: CodeSignal
  • For cell1 = "A1" and cell2 = "H3", the output should be

    chess_board_cell_color(cell1, cell2) = false
    Image Credit: CodeSignal

Solution

py
def chess_board_cell_color(cell1, cell2):
    c1 = (ord(cell1[0]) - 64 + int(cell1[1])) % 2 == 0
    c2 = (ord(cell2[0]) - 64 + int(cell2[1])) % 2 == 0

    return c1 == c2


print(chess_board_cell_color('A1', 'G1'))
js
// c1 = (ord(cell1[0]) - 64 + int(cell1[1])) % 2 == 0
// c2 = (ord(cell2[0]) - 64 + int(cell2[1])) % 2 == 0

// return c1 == c2

function chess_board_cell_color(cell1, cell2) {
  const cell1Color = (cell1.charCodeAt(0) - 64 + parseInt(cell1[1])) % 2 === 0;
  const cell2Color = (cell2.charCodeAt(0) - 64 + parseInt(cell2[1])) % 2 === 0;
  return cell1Color === cell2Color;
}

console.log(chess_board_cell_color('A1', 'G1'));

my thoughts are neither my employer's nor my wife's