Foundations Loops through pattern printing
16. Number Crown
Given an integer n, return the pattern below as a list of strings, one string per row, top row first.
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
That is the pattern for n = 4. The crown has n rows. Row i, counting from 1 at the top, has a left half
and a right half. The left half counts up from 1 to i and sits against the left edge. The right half counts
down from i to 1 and sits against the right edge. Neighbouring numbers in a half are separated by one space.
Every row is as wide as the bottom row, and the gap between the two halves takes up the rest. In the bottom row the gap is a single space. Each row starts and ends with a 1.
Example 1
Input: n = 3
1 1 1 2 2 1 1 2 3 3 2 1
Explanation: Every row is 11 characters wide, the width of the bottom row. The gaps are 9, 5 and 1 spaces.
Example 2
Input: n = 1
1 1Example 3
Input: n = 5
1 1 1 2 2 1 1 2 3 3 2 1 1 2 3 4 4 3 2 1 1 2 3 4 5 5 4 3 2 1
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 9- Every number is one digit, so the two halves of every row line up in columns.
Clarifying questions
What you would ask an interviewer before writing any code.