Determine Color of a Chessboard Square

🏠 ⬅️ ➑️

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

Constraints:

  • coordinates.length == 2
  • 'a' <= coordinates[0] <= 'h'
  • '1' <= coordinates[1] <= '8'

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=2) :: coordinates
    logical :: is_white

    ! Example 1
    coordinates = "a1"
    write(*,*) is_white(coordinates), "Expected: False"

    ! Example 2
    coordinates = "h3"
    write(*,*) is_white(coordinates), "Expected: True"

    ! Example 3
    coordinates = "c7"
    write(*,*) is_white(coordinates), "Expected: False"

contains

    function is_white(coordinates) result(is_white_)
        implicit none
        character(len=*), intent(in) :: coordinates
        integer :: row, col
        logical :: is_white_

        read(coordinates, "(A1,I1)") col, row
        if (col >= 4 .and. col <= 6) then
            is_white_ = .true.
        else
            is_white_ = .false.
        end if
    end function is_white
end program main
❌ Compiled
❌ Executed
❌ Correct
PROGRAM white_square

IMPLICIT NONE

CHARACTER(LEN=2) :: coordinates
LOGICAL :: is_white

! Read the coordinates from the input
READ(*,*) coordinates

! Check if the square is white
is_white = (MOD(ICHAR(coordinates(1)), 2) == 0)

! Print the result
WRITE(*,*) is_white

END PROGRAM white_square
🌐 Data from online sources
def reformat_number(number):
    cleaned_number = ''.join(c for c in number if c.isdigit())

    blocks = []
    n = len(cleaned_number)
    i = 0
    while i < n:
        if n - i == 2 or n - i == 4:
            blocks.append(cleaned_number[i:i + 2])
            i += 2
        else:
            blocks.append(cleaned_number[i:i + 3])
            i += 3

    return '-'.join(blocks)
  1. Remove all non-digit characters from the given phone number string.
  2. Create a list called blocks which will store the number groups.
  3. Loop through the cleaned number string:
  4. If there are exactly 2 or 4 digits remaining, add a group of 2 digits to blocks.
  5. Otherwise, add a group of 3 digits to blocks.
  6. Join the groups in blocks with dashes and return the resulting string.
🌐 Data from online sources
#include <string>
#include <vector>

std::string reformatNumber(std::string number) {
    std::string cleaned_number;
    for (char c : number) {
        if (isdigit(c))
            cleaned_number.push_back(c);
    }

    std::vector<std::string> blocks;
    int n = cleaned_number.size();
    int i = 0;
    while (i < n) {
        if (n - i == 2 || n - i == 4) {
            blocks.push_back(cleaned_number.substr(i, 2));
            i += 2;
        } else {
            blocks.push_back(cleaned_number.substr(i, 3));
            i += 3;
        }
    }

    std::string formatted_number = blocks[0];
    for (int i = 1; i < blocks.size(); i++) {
        formatted_number += '-' + blocks[i];
    }
    return formatted_number;
}
  1. Remove all non-digit characters from the given phone number string.
  2. Create a list called blocks which will store the number groups.
  3. Loop through the cleaned number string:
  4. If there are exactly 2 or 4 digits remaining, add a group of 2 digits to blocks.
  5. Otherwise, add a group of 3 digits to blocks.
  6. Join the groups in blocks with dashes and return the resulting string.