Build a staircase for my kid
Python 2, 55 bytes
i=2
exec"print(i%2*' '+`2%i*1122`*i)[:i];i+=1;"*input()
Try it online!
Cycles between blocks of 22, 44
, except the top row is 00
. For example, on input 10, prints
00
22
2244
2244
224422
224422
22442244
22442244
2244224422
2244224422
Prints rows of increasing length i=2,3,..
by creating a space for odd lengths, repeating the pattern i
times, and trunctating to length i
. The pattern is 2244
for all rows except the first one i=2
for which it's 0
. This is achieved with the arithmetic expression 2%i*1122
.
Jelly, 21 19 16 bytes
d2SR+%3x2⁶;ṙḂµ€Y
A full program printing the result.
Uses 00
, 11
, and 22
as the blocks.
Try it online!
How?
d2SR+%3x2⁶;ṙḂµ€Y - Main link: number n
µ€ - for €ach "row" in n (i.e. for row, r = 1 to n inclusive):
d2 - divmod 2 -> [integer-divide by 2, remainder] i.e. [r/2, r%2]
S - sum -> r/2 + r%2
R - range -> [1, 2, 3, ..., r/2 + r%2]
+ - add r -> [r+1, r+2, r+3, ..., r + r/2 + r%2]
%3 - modulo 3 -> [r%3+1, r%3+2, r%3+0, ..., (r + r/2 + r%2)%3]
- e.g.: r: 1 , 2 , 3 , 4 , 5 , 6 , 7 , ...
[2], [0], [1,2], [2,0], [0,1,2], [1,2,0], [2,0,1,2], ...
x2 - times 2 - repeat each twice (e.g. [2,0,1,2] -> [2,2,0,0,1,1,2,2]
⁶ - literal space character
; - concatenate (add a space character to the left)
Ḃ - r mod 2 (1 if the row is odd, 0 if it is even (1 at the top))
ṙ - rotate (the list) left by (r mod 2)
Y - join with newlines
- implicit print (no brackets printed due to the presence of characters)
JavaScript (ES6), 80 bytes
n=>eval(`for(s=11,i=1;i++<n;)s+='\\n'+(' '+'2233'.repeat(n)).substr(i%2,i+1);s`)
f=
n=>eval(`for(s=11,i=1;i++<n;)s+='\\n'+(' '+'2233'.repeat(n)).substr(i%2,i+1);s`)
console.log(f(+prompt()))
JavaScript (ES6), 87 bytes
Previous solution.
n=>[11,...Array(n).fill(' '+'2233'.repeat(n)).map((r,n)=>r.slice(n%2,n+3+n%2))].join`
`