Given two cells on the standard chess board, determine whether they have the same color or not.
Example
-
For
cell1 = "A1"
andcell2 = "C3"
, the output should bechessBoardCellColor(cell1, cell2) = true
. -
For
cell1 = "A1"
andcell2 = "H3"
, the output should bechessBoardCellColor(cell1, cell2) = false
.
我的解答:
def chessBoardCellColor(cell1, cell2): return abs(int(cell1[1])-int(cell2[1])) % 2 == 0 if abs(ord(cell1[0]) - ord(cell2[0])) % 2 == 0 else abs(int(cell1[1])-int(cell2[1])) % 2 == 1
def chessBoardCellColor(cell1, cell2): return (ord(cell1[0]) + int(cell1[1]) + ord(cell2[0]) + int(cell2[1])) % 2 == 0