Tutte le sfide
Facile 0 risolte 0% di accettazione

Chess Square Color

Determine the color of a square on an 8 by 8 board.

Cosa imparerai

  • Turn a square name into row and column numbers
  • Use parity to decide the color
  • Convert letters to numbers

Problema

Mostra traduzione

Use a board whose top-left square is white. Rows and columns start at 0 at the top left. Adjacent squares alternate colors. Return empty text for a position outside the board.

Implement chess_square_color(column, row).

Input

column: The column, counted from the left starting at 0.

row: The row, counted from the top starting at 0.

Output

Return white, black, or an empty string for invalid coordinates.

Vincoli

−10 ≤ column, row ≤ 20. Valid board coordinates are 0 through 7.

Esempi

Esempio 1
Input:
column = 2
row = 3

Output atteso:
expected = "black"
Input:
long long column = 2;
long long row = 3;

Output atteso:
std::string expected = "black";
Input:
long column = 2;
long row = 3;

Output atteso:
string expected = "black";

Spiegazione

Five steps from the white corner change color an odd number of times.

Esempio 2
Input:
column = 8
row = 0

Output atteso:
expected = ""
Input:
long long column = 8;
long long row = 0;

Output atteso:
std::string expected = "";
Input:
long column = 8;
long row = 0;

Output atteso:
string expected = "";

Spiegazione

Column 8 is outside the valid range 0 through 7.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Square color alternates with moves across rows and columns, so only the coordinate pattern matters.

Suggerimento 2

A parity check on row and column can tell you which color to return.

Suggerimento 3

Return empty text immediately when either coordinate is outside the 0 to 7 board range.

Complessità attesa

O(1) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.