Labels

new blog 2.0

2010/01/24

Python code to recognize a color of a chessboard square

def chesssquare(square):
"""Return the color of a chessboard square."""
rank,file=square
return ord(file) & int(rank) & 1 and 'black' or 'white'

In [1]: chesssquare('a1')
Out [1]: 'black'

In [2]: chesssquare('A2')
Out [2]: 'white'

No comments: