Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Checkerboard Pattern

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

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

Beispiele

Beispiel 1
Eingabe:
rows = 3
cols = 4

Erwartete Ausgabe:
expected = ["#.#.", ".#.#", "#.#."]
Eingabe:
long long rows = 3;
long long cols = 4;

Erwartete Ausgabe:
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
Eingabe:
long rows = 3;
long cols = 4;

Erwartete Ausgabe:
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

Erläuterung

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

Beispiel 2
Eingabe:
rows = 2
cols = 1

Erwartete Ausgabe:
expected = ["#", "."]
Eingabe:
long long rows = 2;
long long cols = 1;

Erwartete Ausgabe:
std::vector<std::string> expected = {"#", "."};
Eingabe:
long rows = 2;
long cols = 1;

Erwartete Ausgabe:
string[] expected = new string[] {"#", "."};

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

Every row has exactly cols characters.

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.