आसान 0 हल किए गए 0% स्वीकृति

Checkerboard Pattern

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

आप क्या सीखेंगे

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

समस्या

अनुवाद दिखाएँ

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.

सीमाएँ

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

उदाहरण

उदाहरण 1
इनपुट:
rows = 3
cols = 4

अपेक्षित आउटपुट:
expected = ["#.#.", ".#.#", "#.#."]
इनपुट:
long long rows = 3;
long long cols = 4;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
इनपुट:
long rows = 3;
long cols = 4;

अपेक्षित आउटपुट:
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

व्याख्या

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

उदाहरण 2
इनपुट:
rows = 2
cols = 1

अपेक्षित आउटपुट:
expected = ["#", "."]
इनपुट:
long long rows = 2;
long long cols = 1;

अपेक्षित आउटपुट:
std::vector<std::string> expected = {"#", "."};
इनपुट:
long rows = 2;
long cols = 1;

अपेक्षित आउटपुट:
string[] expected = new string[] {"#", "."};

व्याख्या

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

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

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

संकेत 2

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

संकेत 3

Every row has exactly cols characters.

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।