[LeetCode]#1812. Determine Color of a Chessboard Square

Fatboy Slim
Jul 20, 2021

--

Environment: Python 3.8

Key technique: if, ord, ASCII
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.

Analysis:

  1. Convert ‘a’ and ‘1’ to ASCII.
  2. If ord(a)+ord(1) is odd, return true. For example, ord(‘a’)+ord(‘1’) =97+ 49=146
  3. Else, return false.

Solution:

class Solution:
def squareIsWhite(self, coordinates):
test=coordinates[0]
number=coordinates[1]
if ((ord(test)+ord(number)) % 2 !=0):
return True
else:
return False

Submissions:

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet