twitter logo github logo ・1 min read

my solution to this code fights problem

generates an odd and even offset board and checks their reference values

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


def chessBoardCellColor(cell1, cell2):
[fooa, foob] = cell1
[bara, barb] = cell2

fooa = ord(fooa) % 2 
foob = int(foob) % 2

bara = ord(bara) % 2 
barb = int(barb) % 2

board = [[(i+1)%2 for i in range(0,9)], [i%2 for i in range(0,9)]]

return True if board[fooa][foob] == board[bara][barb] else False


twitter logo DISCUSS (1)
markdown guide

Given that there are mathematical ways to compute their equality, you could probably skip the part where you generate the board.
A solution could be to just look at the cell indexes and how they relate to each other:

def color_equal(c1, c2):
    return ((c1[0] + c1[1]) % 2) == ((c2[0] + c2[1]) % 2)

>>> color_equal((1,1),(3,3))
>>> color_equal((1,1),(3,4))
Classic DEV Post from May 10 '19

Why I ignore the hype (and you should too)

It’s been 20 years since I made my first website. I've been burned by the hype ti...

Stanley Zheng profile image
Stanley Zheng is a software engineer and technologist who loves coding as much as he enjoys tech communities. I attended the Recurse Center in NYC apply if you want to become a better programmer!