Foundations Loops through pattern printing
15. Alternating Binary Triangle
Given an integer n, return the pattern below as a list of rows, top row first. Each row is a list of the
numbers in it, from left to right.
1
0 1
1 0 1
0 1 0 1
The pattern has n rows; the picture shows n = 4. Row i, counting from 1, holds i numbers, and each of
them is 1 or 0. Neighbouring numbers in a row are always different. Odd-numbered rows start with 1, and
even-numbered rows start with 0. For n = 4 the answer is [[1],[0,1],[1,0,1],[0,1,0,1]].
Example 1
Input: n = 4
1 0 1 1 0 1 0 1 0 1
Explanation: Rows 1 and 3 start with 1, and rows 2 and 4 start with 0.
Example 2
Input: n = 1
1Example 3
Input: n = 7
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1
Explanation: Row 6 ends with 1, and row 7 starts with 1 as well, because each row starts by its own rule.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 100
Clarifying questions
What you would ask an interviewer before writing any code.