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'
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
temp.f95:21:22: 21 | function is_white(coordinates) result(is_white_) | 1 Error: Procedure βis_whiteβ at (1) has an explicit interface from a previous declaration temp.f95:22:21: 22 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:23:51: 23 | character(len=*), intent(in) :: coordinates | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:27: 24 | integer :: row, col | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:28: 25 | logical :: is_white_ | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:45: 27 | read(coordinates, "(A1,I1)") col, row | 1 Error: Unexpected READ statement in CONTAINS section at (1) temp.f95:28:41: 28 | if (col >= 4 .and. col <= 6) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:29:30: 29 | is_white_ = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:12: 30 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:31:31: 31 | is_white_ = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:32:11: 32 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:7: 33 | end function is_white | 1 Error: Expecting END PROGRAM statement at (1)
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
temp.f95:12:34: 12 | is_white = (MOD(ICHAR(coordinates(1)), 2) == 0) | 1 Error: Syntax error in argument list at (1)
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)
blocks
which will store the number groups.blocks
.blocks
.blocks
with dashes and return the resulting string.#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;
}
blocks
which will store the number groups.blocks
.blocks
.blocks
with dashes and return the resulting string.