Tous les défis
Facile 0 résolus 0 % de réussite

Checkerboard Pattern

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

Exemples

Exemple 1
Entrée :
rows = 3
cols = 4

Sortie attendue :
expected = ["#.#.", ".#.#", "#.#."]
Entrée :
long long rows = 3;
long long cols = 4;

Sortie attendue :
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
Entrée :
long rows = 3;
long cols = 4;

Sortie attendue :
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

Explication

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

Exemple 2
Entrée :
rows = 2
cols = 1

Sortie attendue :
expected = ["#", "."]
Entrée :
long long rows = 2;
long long cols = 1;

Sortie attendue :
std::vector<std::string> expected = {"#", "."};
Entrée :
long rows = 2;
long cols = 1;

Sortie attendue :
string[] expected = new string[] {"#", "."};

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

Every row has exactly cols characters.

Complexité attendue

O(rows * cols) time, O(rows * cols) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.