[Leet Code] Determine Color of a Chessboard Square

Matthew Boyd
2 min readOct 16, 2021

--

Leetcode: https://leetcode.com/problems/determine-color-of-a-chessboard-square/

Problem:

You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.

The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.

Example 1:

Input: coordinates = "a1"
Output: false
Explanation: From the chessboard above, the square with coordinates "a1" is black, so return false.

Example 2:

Input: coordinates = "h3"
Output: true
Explanation: From the chessboard above, the square with coordinates "h3" is white, so return true.

Example 3:

Input: coordinates = "c7"
Output: false

Solution:

class Solution(object):
def squareIsWhite(self, coordinates):
"""
:type coordinates: str
:rtype: bool
"""
values = {"a":1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8}
if ((values[coordinates[0]] % 2 == 0 and int(coordinates[1]) % 2 == 0) or (int(values[coordinates[0]]) % 2 != 0 and int(coordinates[1]) % 2 != 0)):
return False
elif ((values[coordinates[0]] % 2 == 0 and int(coordinates[1]) % 2 != 0) or (values[coordinates[0]] % 2 != 0 and int(coordinates[1]) % 2 == 0)):
return True

--

--

Matthew Boyd
Matthew Boyd

Written by Matthew Boyd

Learning, and posting my findings!

No responses yet