Tutte le sfide
Facile 0 risolte 0% di accettazione

Checkerboard Pattern

Draw a checkerboard of # and . cells with the given number of rows and columns.

Cosa imparerai

  • Use r + c to decide between two alternating values
  • Build a rectangular grid row by row
  • Handle a grid with zero rows

Problema

Mostra traduzione

A checkerboard alternates two colors in both directions. Here a dark cell is # and a light cell is .. Number rows and columns from 0; the cell in row r and column c is # when r + c is even and . when it is odd, so the top-left cell is always #.

Implement checkerboard_pattern(rows, cols).

Input

rows: The number of rows.

cols: The number of columns.

Output

Return rows strings from top to bottom, each exactly cols characters long with no spaces. When rows is 0, return an empty list.

Vincoli

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

Esempi

Esempio 1
Input:
rows = 3
cols = 4

Output atteso:
expected = ["#.#.", ".#.#", "#.#."]
Input:
long long rows = 3;
long long cols = 4;

Output atteso:
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
Input:
long rows = 3;
long cols = 4;

Output atteso:
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

Spiegazione

Row 0 starts with #, row 1 with ., row 2 with # again: ["#.#.", ".#.#", "#.#."].

Esempio 2
Input:
rows = 2
cols = 1

Output atteso:
expected = ["#", "."]
Input:
long long rows = 2;
long long cols = 1;

Output atteso:
std::vector<std::string> expected = {"#", "."};
Input:
long rows = 2;
long cols = 1;

Output atteso:
string[] expected = new string[] {"#", "."};

Spiegazione

One column: cell (0, 0) has an even sum and cell (1, 0) an odd one: ["#", "."].

Come affrontarlo

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

Suggerimento 1

Neighbouring cells differ by one in either r or c, so r + c changes parity.

Suggerimento 2

Decide each cell from (r + c) mod 2.

Suggerimento 3

Every row has exactly cols characters.

Complessità attesa

O(rows * cols) time, O(rows * cols) 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.