Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 19, 2017 14:39
Show Gist options
  • Save tyler-austin/566839512a465fb8b0660a880e5ae996 to your computer and use it in GitHub Desktop.
Save tyler-austin/566839512a465fb8b0660a880e5ae996 to your computer and use it in GitHub Desktop.

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
chessBoardCellColor(cell1, cell2) = true.

For cell1 = "A1" and cell2 = "H3", the output should be
chessBoardCellColor(cell1, cell2) = false.

Input/Output

  • [time limit] 4000ms (py3)

  • [input] string cell1

  • [input] string cell2

  • [output] boolean

    true if both cells have the same color, false otherwise.

def chess_board_cell_color(cell1, cell2):
if _is_black(cell1) == _is_black(cell2):
return True
return False
def _is_black(cell):
return (ord(cell[0]) % 2 != 0 and int(cell[1]) % 2 != 0) or \
(ord(cell[0]) % 2 == 0 and int(cell[1]) % 2 == 0)
import unittest
from chess_board_cell_color import chess_board_cell_color
class TestChessBoardCellColor(unittest.TestCase):
def test_1(self):
cell1 = 'A1'
cell2 = 'C3'
result = chess_board_cell_color(cell1, cell2)
self.assertTrue(result)
def test_2(self):
cell1 = 'A1'
cell2 = 'H3'
result = chess_board_cell_color(cell1, cell2)
self.assertFalse(result)
def test_3(self):
cell1 = 'A1'
cell2 = 'A2'
result = chess_board_cell_color(cell1, cell2)
self.assertFalse(result)
def test_4(self):
cell1 = 'A1'
cell2 = 'B2'
result = chess_board_cell_color(cell1, cell2)
self.assertTrue(result)
def test_5(self):
cell1 = 'B3'
cell2 = 'H8'
result = chess_board_cell_color(cell1, cell2)
self.assertFalse(result)
def test_6(self):
cell1 = 'C3'
cell2 = 'B5'
result = chess_board_cell_color(cell1, cell2)
self.assertFalse(result)
def test_7(self):
cell1 = 'G5'
cell2 = 'E7'
result = chess_board_cell_color(cell1, cell2)
self.assertTrue(result)
def test_8(self):
cell1 = 'C8'
cell2 = 'H8'
result = chess_board_cell_color(cell1, cell2)
self.assertFalse(result)
def test_9(self):
cell1 = 'D2'
cell2 = 'D2'
result = chess_board_cell_color(cell1, cell2)
self.assertTrue(result)
def test_10(self):
cell1 = 'A2'
cell2 = 'A5'
result = chess_board_cell_color(cell1, cell2)
self.assertFalse(result)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment